Question

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
Was it helpful?

Solution

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']))

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top