TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Stripping non alpha numeric characters (http://www.talkphp.com/general/5142-stripping-non-alpha-numeric-characters.html)

h0ly lag 12-03-2009 02:28 AM

Stripping non alpha numeric characters
 
This should work correct?

PHP Code:

ereg_replace('[^A-Za-z]',''$str); 

Because I am using it on a project that I need to strip any characters that are not alpha numeric, or if the string contains dash's or underscores that is ok too. But I can add that in later. I guess my problem is that It's not stripping '^' or '#'. I'm sure there are more characters its letting through. But those are the ones I've noticed so far.

delayedinsanity 12-03-2009 04:10 AM

You should avoid using ereg_replace() - the ereg_* set of functions have been deprecated in favor of the PCRE equivalent preg_* set. They're also much slower.

With that out of the way, it should work.

php Code:
$string = 'This $is a #test ~of ^the non-alphanumeric preg_replace';
echo preg_replace( '~[^A-Za-z0-9\s\-_]~', '', $string );

h0ly lag 12-03-2009 06:13 AM

Oh dang. I should know that about ereg. I just read that the other day. and thank you very much. I know if I ever have a problem you guys are always super friendly at TalkPHP. :) Thanks again.


All times are GMT. The time now is 09:19 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0