hello can i filter the string using regular expression. example like this $patern = "/^[a-zA-Z0-9\ ]+$/"; if(preg_match($patern,$some_random_string)) { // some statement } if i do like that. it only check the string true or false right? . my question is how can i remove the string except letter and number only. if i do like this $some_random_string = str_replace('#;','',$some_random_string ); $some_random_string = str_replace("'',"",$some_random_string); it not very nice because the script too long. that is only 2 string i remove. what about if i want to remove many string. the script will be so long. and hard to edit back later. if i can remove string using regular expression it will be great. i try like this $patern = "/^[a-zA-Z0-9\ ]+$/"; $res = preg_match($patern,$some_random_string); but the output show 0 or 1 . means true or false. it not filter that string. sorry if my english bad.
If regex confuses the hell out of you (like it does me), you can always do this: $dirty = array( "#", ":", "?" ); $string = str_replace($dirty, "", $string); PHP: That will remove each occurrence of each item in $dirty.
Well the regex is fairly easy to understand. If a character isn't a-z or 0-9 then replace it with '' (or a null string) If you were to do it you way then it would be easier to have $dirty = str_split('#:?'); PHP:
Ahh, my bad - misread the question. I thought there were just specific characters that needed removing. Yes, if it's everything-but, regex is definitely the way to go.