-1

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
1
  • 4
    You mean like "/[^ \w,]+/".... is this really rocket science? Commented Mar 3, 2014 at 12:16

2 Answers 2

2

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']))
Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

This would change what is qualified as a match since \w also matches underscores.
yes correct , based on assumption he need only alphabets and , i have written this regex
Still, he/she may need them to be included for one reason or another so you should at least warn the op that the matches may be different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.