-4
$str = a b c;
$str = preg_replace("/a|b|c\","" $str);

above regex only matched a, b and c are excluded. At first I thought it was caused by the gobal thing, but after researched preg_match itself has default global enabled. So what has actually gone wrong?

2 Answers2

0

You'll need quotes around your string:

$str = "a b c";

...a comma between your replacement text and the source, and flip your closing RegEx slash to match your opening one:

$str = preg_replace("/a|b|c/", "", $str);

That will leave $str set to [space][space]

Will
  • 2,343
  • 1
  • 14
  • 14
0

A regex must be enclosed in delimeters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character. Usually you use / which has to be at the beginning and at the end of the regex string.

Also a string in php must be encased in either " or '.

Lastly you are missing a comma in the preg_replace function.

$str = "a b c";
$str = preg_replace("/a|b|c/", "", $str);
Ke Vin
  • 2,004
  • 1
  • 18
  • 28