regexp to leave only alpha/numeric chars

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

    regexp to leave only alpha/numeric chars

    Using preg_replace() is there a simple regexp to strip everything from a
    string except alpha and numeric chars (a-zA-Z0-9)?

    $input = "$tring1!";
    $pattern =
    $input = preg_replace($p attern, "", $input);

    result: "tring1"



  • Kenneth Downs

    #2
    Re: regexp to leave only alpha/numeric chars

    Bosconian wrote:
    [color=blue]
    > Using preg_replace() is there a simple regexp to strip everything from a
    > string except alpha and numeric chars (a-zA-Z0-9)?
    >
    > $input = "$tring1!";
    > $pattern =
    > $input = preg_replace($p attern, "", $input);
    >
    > result: "tring1"[/color]

    Chances are anything you will ever try to do with regexp's has been done,
    and there may even be a shortcut for it. So at this page:



    you would be able to find this:

    $input = preg_replace('\ W','',$input)

    This will leave in underscores, but that is easily fixed:

    $input = preg_replace('[\W_]','',$input)

    Also try reading an introduction to regexp's in a Perl guide, you can likely
    find something more of a tutorial there to get you started.

    --
    Kenneth Downs
    Secure Data Software, Inc.
    (Ken)nneth@(Sec )ure(Dat)a(.com )

    Comment

    • Bosconian

      #3
      Re: regexp to leave only alpha/numeric chars

      "Kenneth Downs" <knode.wants.th is@see.sigblock > wrote in message
      news:3ifrj2-q7n.ln1@pluto.d ownsfam.net...[color=blue]
      > Bosconian wrote:
      >[color=green]
      > > Using preg_replace() is there a simple regexp to strip everything from a
      > > string except alpha and numeric chars (a-zA-Z0-9)?
      > >
      > > $input = "$tring1!";
      > > $pattern =
      > > $input = preg_replace($p attern, "", $input);
      > >
      > > result: "tring1"[/color]
      >
      > Chances are anything you will ever try to do with regexp's has been done,
      > and there may even be a shortcut for it. So at this page:
      >
      > http://www.php.net/manual/en/referen...ern.syntax.php
      >
      > you would be able to find this:
      >
      > $input = preg_replace('\ W','',$input)
      >
      > This will leave in underscores, but that is easily fixed:
      >
      > $input = preg_replace('[\W_]','',$input)
      >
      > Also try reading an introduction to regexp's in a Perl guide, you can[/color]
      likely[color=blue]
      > find something more of a tutorial there to get you started.
      >
      > --
      > Kenneth Downs
      > Secure Data Software, Inc.
      > (Ken)nneth@(Sec )ure(Dat)a(.com )[/color]

      Ken,

      I was able to use your example, but not without adding starting and ending
      delimiters:

      '/[\W_]/'

      Nothing was stripped without them.

      I will bone-up on my regexp syntax.

      Many thanks.


      Comment

      • Ewoud Dronkert

        #4
        Re: regexp to leave only alpha/numeric chars

        Bosconian wrote:[color=blue]
        > I was able to use your example, but not without adding starting and ending
        > delimiters: '/[\W_]/'[/color]

        You might find '/[\W_]+/' faster. Also, on the linked doc page it says
        the \w and \W are locale specific; there might be accented characters
        left behind. If you don't want that, use '/[^a-z0-9]+/i'.


        --
        Firefox Web Browser - Rediscover the web - http://getffox.com/
        Thunderbird E-mail and Newsgroups - http://gettbird.com/

        Comment

        • Kenneth Downs

          #5
          Re: regexp to leave only alpha/numeric chars

          Bosconian wrote:
          [color=blue]
          > "Kenneth Downs" <knode.wants.th is@see.sigblock > wrote in message
          > news:3ifrj2-q7n.ln1@pluto.d ownsfam.net...[color=green]
          >> Bosconian wrote:
          >>[color=darkred]
          >> > Using preg_replace() is there a simple regexp to strip everything from
          >> > a string except alpha and numeric chars (a-zA-Z0-9)?
          >> >
          >> > $input = "$tring1!";
          >> > $pattern =
          >> > $input = preg_replace($p attern, "", $input);
          >> >
          >> > result: "tring1"[/color]
          >>
          >> Chances are anything you will ever try to do with regexp's has been done,
          >> and there may even be a shortcut for it. So at this page:
          >>
          >> http://www.php.net/manual/en/referen...ern.syntax.php
          >>
          >> you would be able to find this:
          >>
          >> $input = preg_replace('\ W','',$input)
          >>
          >> This will leave in underscores, but that is easily fixed:
          >>
          >> $input = preg_replace('[\W_]','',$input)
          >>
          >> Also try reading an introduction to regexp's in a Perl guide, you can[/color]
          > likely[color=green]
          >> find something more of a tutorial there to get you started.
          >>
          >> --
          >> Kenneth Downs
          >> Secure Data Software, Inc.
          >> (Ken)nneth@(Sec )ure(Dat)a(.com )[/color]
          >
          > Ken,
          >
          > I was able to use your example, but not without adding starting and ending
          > delimiters:
          >
          > '/[\W_]/'
          >
          > Nothing was stripped without them.
          >
          > I will bone-up on my regexp syntax.
          >
          > Many thanks.[/color]

          I would love to say I left the delimiters out on purpose to improve your
          skills, but it would be a lie. I just forgot.

          Glad it helped.


          --
          Kenneth Downs
          Secure Data Software, Inc.
          (Ken)nneth@(Sec )ure(Dat)a(.com )

          Comment

          • Bosconian

            #6
            Re: regexp to leave only alpha/numeric chars

            "Ewoud Dronkert" <firstname@last name.net.invali d> wrote in message
            news:426b5a6e$0 $157$e4fe514c@d reader4.news.xs 4all.nl...[color=blue]
            > Bosconian wrote:[color=green]
            > > I was able to use your example, but not without adding starting and[/color][/color]
            ending[color=blue][color=green]
            > > delimiters: '/[\W_]/'[/color]
            >
            > You might find '/[\W_]+/' faster. Also, on the linked doc page it says
            > the \w and \W are locale specific; there might be accented characters
            > left behind. If you don't want that, use '/[^a-z0-9]+/i'.
            >
            >
            > --
            > Firefox Web Browser - Rediscover the web - http://getffox.com/
            > Thunderbird E-mail and Newsgroups - http://gettbird.com/[/color]

            Even better. Thanks for the tip.


            Comment

            Working...