This is a discussion on regexp to leave only alpha/numeric chars within the PHP Language forums, part of the PHP Programming Forums category; Using preg_replace() is there a simple regexp to strip everything from a string except alpha and numeric chars (a-zA-...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Bosconian wrote:
> 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($pattern, "", $input); > > result: "tring1" 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 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) |
|
|||
|
"Kenneth Downs" <knode.wants.this@see.sigblock> wrote in message
news:3ifrj2-q7n.ln1@pluto.downsfam.net... > Bosconian wrote: > > > 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($pattern, "", $input); > > > > result: "tring1" > > 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 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) 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. |
|
|||
|
Bosconian wrote:
> I was able to use your example, but not without adding starting and ending > delimiters: '/[\W_]/' 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/ |
|
|||
|
Bosconian wrote:
> "Kenneth Downs" <knode.wants.this@see.sigblock> wrote in message > news:3ifrj2-q7n.ln1@pluto.downsfam.net... >> Bosconian wrote: >> >> > 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($pattern, "", $input); >> > >> > result: "tring1" >> >> 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 > 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) > > 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. 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) |
|
|||
|
"Ewoud Dronkert" <firstname@lastname.net.invalid> wrote in message
news:426b5a6e$0$157$e4fe514c@dreader4.news.xs4all. nl... > Bosconian wrote: > > I was able to use your example, but not without adding starting and ending > > delimiters: '/[\W_]/' > > 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/ Even better. Thanks for the tip. |