By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

regexp to leave only alpha/numeric chars

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
"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.