View Full Version : How to remove special character?
medica
April 22nd, 2008, 12:14 AM
I want to remove special charcters, say ' ( ) : ?
only remain letters, numbers, and blank space
kdd
April 22nd, 2008, 01:31 AM
What language? I'm assuming PHP, then:
$arr = array ( "'" , "(" , ")" , ":" , "?" );
$newStr = str_replace ( $arr , "" , $oldStr );
But can anyone else do this same thing using trim, ltrim, and rtrim functions. I'd like to see that. Thanks! :)
PHP's array and string libraries are so huge, it's hard to remember all this stuff. It's like you have to keep one window open just for that!
simplistik
April 22nd, 2008, 09:34 AM
use regex instead, it's more effective since you're providing a white list as opposed to a black list
ereg_replace('[^A-Za-z0-9 ]','',$str);
example
<?php
$str = '()-abc123 d 35#$#%^^&';
$str = ereg_replace('[^A-Za-z0-9 ]','',$str);
echo $str;
?>
output
abc123 d 35
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.