468,308 Members | 1,267 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,308 developers. It's quick & easy.

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"

Jul 17 '05 #1
5 8808
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)
Jul 17 '05 #2
"Kenneth Downs" <kn**************@see.sigblock> wrote in message
news:3i************@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.
Jul 17 '05 #3
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/
Jul 17 '05 #4
Bosconian wrote:
"Kenneth Downs" <kn**************@see.sigblock> wrote in message
news:3i************@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)
Jul 17 '05 #5
"Ewoud Dronkert" <fi*******@lastname.net.invalid> wrote in message
news:42*********************@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.
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by John K. Humkey | last post: by
reply views Thread by Ed Leafe | last post: by
7 posts views Thread by Federico G. Babelis | last post: by
3 posts views Thread by success_ny | last post: by
6 posts views Thread by Edward | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.