Removal of special characters, a comma separated text, PHP
I got my text variable which is user-specified, normally, user should enter the tags which has to look following:
开发者_如何学JAVA"food, community, relationship"
but if user type for example
"food;;[]'.'.;@$#community,,,-;,,,relationship"
the script should change it into:
"food, community, relationship".
How can I get this done?
how about:
$str = "-----music,,,,,,,,games;'235@#%@#%media";
$arr = preg_split("/\W+/", $str, -1, PREG_SPLIT_NO_EMPTY);
$str = implode(', ', $arr);
echo $str,"\n";
output:
music, games, 235, media
You could adapt the \W
to which characters you need to keep.
You could use something like this:
$content = preg_replace("/[a-zA-Z,]/", "", $content);
$content = str_replace(",,", ",", $content);
$content = str_replace(",", " ,", $content);
You could replace everything that isnt alphanumeric, as such:
preg_split('/\W+/','',$input);
will output
Array ( [0] => food [1] => community [2] => relationship [3] => 1123123123 )
Edit: fixed regex
精彩评论