Stripping Data Help Needed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ralph Freshour

    #1

    Stripping Data Help Needed

    I'm taking text from a textarea object and want to strip out
    characters other than A-Za-z0-9 before I send the data to MySQL table
    - is there a function I can use to do this in PHP?

    Thanks...

  • Tom Thackrey

    #2
    Re: Stripping Data Help Needed


    On 12-Oct-2003, Ralph Freshour <ralph@primemai l.com> wrote:
    [color=blue]
    > I'm taking text from a textarea object and want to strip out
    > characters other than A-Za-z0-9 before I send the data to MySQL table
    > - is there a function I can use to do this in PHP?[/color]

    $strippedtext = preg_replace('/[^0-9a-z]/i','',$original text);

    --
    Tom Thackrey

    tom (at) creative (dash) light (dot) com
    do NOT send email to jamesbutler@wil lglen.net (it's reserved for spammers)

    Comment

    • Ralph Freshour

      #3
      Re: Stripping Data Help Needed

      How would I handle special characters like single and double quotes?
      forward and backward slashes?

      Thanks...

      On Mon, 13 Oct 2003 01:25:34 GMT, "Tom Thackrey"
      <use.signature@ nospam.com> wrote:
      [color=blue]
      >
      >On 12-Oct-2003, Ralph Freshour <ralph@primemai l.com> wrote:
      >[color=green]
      >> I'm taking text from a textarea object and want to strip out
      >> characters other than A-Za-z0-9 before I send the data to MySQL table
      >> - is there a function I can use to do this in PHP?[/color]
      >
      >$strippedtex t = preg_replace('/[^0-9a-z]/i','',$original text);[/color]

      Comment

      • Tom Thackrey

        #4
        Re: Stripping Data Help Needed


        On 12-Oct-2003, Ralph Freshour <ralph@primemai l.com> wrote:
        [color=blue]
        > On Mon, 13 Oct 2003 01:25:34 GMT, "Tom Thackrey"
        > <use.signature@ nospam.com> wrote:
        >[color=green]
        > >
        > >On 12-Oct-2003, Ralph Freshour <ralph@primemai l.com> wrote:
        > >[color=darkred]
        > >> I'm taking text from a textarea object and want to strip out
        > >> characters other than A-Za-z0-9 before I send the data to MySQL table
        > >> - is there a function I can use to do this in PHP?[/color]
        > >
        > >$strippedtex t = preg_replace('/[^0-9a-z]/i','',$original text);[/color][/color]
        [color=blue]
        > How would I handle special characters like single and double quotes?
        > forward and backward slashes?
        >[/color]

        That will strip them out, too. It will remove everything but letters and
        numbers from the original string. What the regular expression says is
        replace every occurance of a character that is not (^) a number (0-9) or a
        letter (a-z) both upper and lower case (i) with a null ('').

        If you want to just make the string mysql safe but keep the punctuation use
        addslashes().

        --
        Tom Thackrey

        tom (at) creative (dash) light (dot) com
        do NOT send email to jamesbutler@wil lglen.net (it's reserved for spammers)

        Comment

        • Ralph Freshour

          #5
          Re: Stripping Data Help Needed

          I'm sorry I didn't think this through further Tom - while I wanted
          initially 0-9a-z etc., after using this in a form (it worked really
          neat!!), I realized that I needed to allow additional specific
          characters such as {}[]<>

          Ralph

          On Mon, 13 Oct 2003 02:30:14 GMT, "Tom Thackrey"
          <use.signature@ nospam.com> wrote:
          [color=blue]
          >
          >On 12-Oct-2003, Ralph Freshour <ralph@primemai l.com> wrote:
          >[color=green]
          >> On Mon, 13 Oct 2003 01:25:34 GMT, "Tom Thackrey"
          >> <use.signature@ nospam.com> wrote:
          >>[color=darkred]
          >> >
          >> >On 12-Oct-2003, Ralph Freshour <ralph@primemai l.com> wrote:
          >> >
          >> >> I'm taking text from a textarea object and want to strip out
          >> >> characters other than A-Za-z0-9 before I send the data to MySQL table
          >> >> - is there a function I can use to do this in PHP?
          >> >
          >> >$strippedtex t = preg_replace('/[^0-9a-z]/i','',$original text);[/color][/color]
          >[color=green]
          >> How would I handle special characters like single and double quotes?
          >> forward and backward slashes?
          >>[/color]
          >
          >That will strip them out, too. It will remove everything but letters and
          >numbers from the original string. What the regular expression says is
          >replace every occurance of a character that is not (^) a number (0-9) or a
          >letter (a-z) both upper and lower case (i) with a null ('').
          >
          >If you want to just make the string mysql safe but keep the punctuation use
          >addslashes() .[/color]

          Comment

          • Tom Thackrey

            #6
            Re: Stripping Data Help Needed


            On 12-Oct-2003, Ralph Freshour <ralph@primemai l.com> wrote:
            [color=blue]
            > On Mon, 13 Oct 2003 02:30:14 GMT, "Tom Thackrey"
            > <use.signature@ nospam.com> wrote:
            >[color=green]
            > >
            > >On 12-Oct-2003, Ralph Freshour <ralph@primemai l.com> wrote:
            > >[color=darkred]
            > >> On Mon, 13 Oct 2003 01:25:34 GMT, "Tom Thackrey"
            > >> <use.signature@ nospam.com> wrote:
            > >>
            > >> >
            > >> >On 12-Oct-2003, Ralph Freshour <ralph@primemai l.com> wrote:
            > >> >
            > >> >> I'm taking text from a textarea object and want to strip out
            > >> >> characters other than A-Za-z0-9 before I send the data to MySQL
            > >> >> table
            > >> >> - is there a function I can use to do this in PHP?
            > >> >
            > >> >$strippedtex t = preg_replace('/[^0-9a-z]/i','',$original text);[/color]
            > >[color=darkred]
            > >> How would I handle special characters like single and double quotes?
            > >> forward and backward slashes?
            > >>[/color]
            > >
            > >That will strip them out, too. It will remove everything but letters and
            > >numbers from the original string. What the regular expression says is
            > >replace every occurance of a character that is not (^) a number (0-9) or
            > >a
            > >letter (a-z) both upper and lower case (i) with a null ('').
            > >
            > >If you want to just make the string mysql safe but keep the punctuation
            > >use
            > >addslashes() .[/color][/color]
            [color=blue]
            > I'm sorry I didn't think this through further Tom - while I wanted
            > initially 0-9a-z etc., after using this in a form (it worked really
            > neat!!), I realized that I needed to allow additional specific
            > characters such as {}[]<>
            >[/color]

            all you have to do is add the extra characters to the pattern, except that
            some of them need to be escaped like () and []

            $strippedtext = preg_replace('/[^0-9a-z\[\]\(\)<>]/i','',$original text);

            see

            PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


            --
            Tom Thackrey

            tom (at) creative (dash) light (dot) com
            do NOT send email to jamesbutler@wil lglen.net (it's reserved for spammers)

            Comment

            • marius ursache

              #7
              Re: Stripping Data Help Needed

              Ralph Freshour wrote:
              [color=blue]
              > I'm taking text from a textarea object and want to strip out
              > characters other than A-Za-z0-9 before I send the data to MySQL table
              > - is there a function I can use to do this in PHP?
              >
              > Thanks...[/color]

              maybe you need striptags (from php)?

              --
              marius

              Free your mind; the rest will follow.

              Comment

              Working...