Re: Stripping Characters

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 2010-06-22 at 11:40 -0400, Rick Dwyer wrote:

> Hello List.
> 
> I need to remove characters from a string and replace them with and  
> underscore.
> 
> So instead of having something like:
> 
> $moditem = str_replace("--","_","$mystring");
> $moditem = str_replace("?","_","$mystring");
> $moditem = str_replace("!","_","$mystring");
> ....etc.
> 
> For every possible character I can think of, is there a way to simply  
> omit any character that is not an alpha character and not a number  
> value from 0 to 9?
> 
> 
>   --Rick
> 
> 
> 


Use preg_replace(), which allows you to use a regex to specify what you
want to match:

$find = '/[^a-z0-9]/i';
$replace = '_';
$new_string = preg_replace($find, $replace, $old_string);

Thanks,
Ash
http://www.ashleysheridan.co.uk



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux