Function to strip non-alphanumeric chars...

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

    Function to strip non-alphanumeric chars...

    Hello All,
    I am writing a function that is php 4.4.1 compatible to remove any char
    that is not alnum. My question is, is there a built in function in PHP
    that will do this for me?

    Thanks!
    Cmac

  • Justin Koivisto

    #2
    Re: Function to strip non-alphanumeric chars...

    Cmac wrote:[color=blue]
    > Hello All,
    > I am writing a function that is php 4.4.1 compatible to remove any char
    > that is not alnum. My question is, is there a built in function in PHP
    > that will do this for me?[/color]

    preg_replace('`[^a-z0-9]`i','',$string) ;

    --
    Justin Koivisto, ZCE - justin@koivi.co m

    Comment

    • d

      #3
      Re: Function to strip non-alphanumeric chars...

      "Cmac" <cordmcphail@gm ail.com> wrote in message
      news:1140625972 .085726.99790@g 14g2000cwa.goog legroups.com...[color=blue]
      > Hello All,
      > I am writing a function that is php 4.4.1 compatible to remove any char
      > that is not alnum. My question is, is there a built in function in PHP
      > that will do this for me?[/color]

      $string=preg_re place("/[^a-z\d]/i", "", $string);

      that'll do it :)
      [color=blue]
      > Thanks!
      > Cmac
      >[/color]


      Comment

      • Cmac

        #4
        Re: Function to strip non-alphanumeric chars...

        Thanks :D !!!

        Comment

        • Cmac

          #5
          Re: Function to strip non-alphanumeric chars...

          You know, I forgot to mention that I did not want to remove spaces, so
          I did this...

          $string=preg_re place("/[^a-z \d]/i", "", $string);

          and it worked swimmingly :D

          thanks again!

          Comment

          • Cmac

            #6
            Re: Function to strip non-alphanumeric chars...

            SO for my own benefit and for posterity's sake:

            preg_replace() is Perl Compatible Regular Expression (PCRE) function to
            replace a match:

            preg_replace(pa ttern,replaceme nt,haystack)

            in this RegEx /[^a-z \d]/i

            [match a-z SPACE and any decimal number] / CASEINSENSITIVE switch.

            Thanks a lot all!

            Comment

            • Janwillem Borleffs

              #7
              Re: Function to strip non-alphanumeric chars...

              Cmac wrote:[color=blue]
              > in this RegEx /[^a-z \d]/i
              >
              > [match a-z SPACE and any decimal number] / CASEINSENSITIVE switch.
              >[/color]

              Actually, this means [*do not* match a-z SPACE and any decimal number]


              JW


              Comment

              • Cmac

                #8
                Re: Function to strip non-alphanumeric chars...

                see, now I know more ;^) -thanks!

                Comment

                Working...