質問

This is my code

$keywords = mysql_escape_string(preg_replace("/[^ \w]+/", '',($_POST['keywords'])));

and this my output

sample products products sample

Here I don't want to replace comma also.

The input is

sample products!@, products#$#, sample
役に立ちましたか?

解決

If you don't want to get rid of ,s, you can add them to your group in the square brackets:

preg_replace("/[^, \w]+/", '',($_POST['keywords']))

他のヒント

try this

$keywords = preg_replace("/[^A-Z, ]/i", '',($_POST['keywords']));
echo $keywords;

Demo

Note: this replaces the content which does not match with the regex. Here it replaces anything except alpabets(case in-sensitive ) and comma , and blank space

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top