regexp to leave only alpha/numeric chars

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-...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-24-2005
Bosconian
 
Posts: n/a
Default 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($pattern, "", $input);

result: "tring1"



Reply With Quote
  #2 (permalink)  
Old 04-24-2005
Kenneth Downs
 
Posts: n/a
Default Re: regexp to leave only alpha/numeric chars

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)
Reply With Quote
  #3 (permalink)  
Old 04-24-2005
Bosconian
 
Posts: n/a
Default Re: regexp to leave only alpha/numeric chars

"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.


Reply With Quote
  #4 (permalink)  
Old 04-24-2005
Ewoud Dronkert
 
Posts: n/a
Default Re: regexp to leave only alpha/numeric chars

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/
Reply With Quote
  #5 (permalink)  
Old 04-24-2005
Kenneth Downs
 
Posts: n/a
Default Re: regexp to leave only alpha/numeric chars

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)
Reply With Quote
  #6 (permalink)  
Old 04-27-2005
Bosconian
 
Posts: n/a
Default Re: regexp to leave only alpha/numeric chars

"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.


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:18 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0