Domanda

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
È stato utile?

Soluzione

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

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top