问题
I am trying to use a regular expression to replace any domain in a string with another domain but it isn't working yet.
I tested the RegEx part on regexpal.com and it seems to be working.
Here is my code:
$itemdesc = str_replace("([a-z0-9\-]+).(com|net|org|co|cm|info|cc)\s\i","Example.com",$itemdesc);
Please Help! Thanks in advance
回答1:
You need delimiters and preg_replace:
$itemdesc = preg_replace("/([a-z0-9\-]+)\.(com|net|org|co|cm|info|cc)/si","Example.com",$itemdesc);
Note the slashes at either end - the modifiers following the last delimiter
Also note, the delimiters can be any character - try to use a character that isn't used in your regular expression, otherwise it'll need to be escaped everywhere.
回答2:
str_replace does not take a regular expression as parameter, you have to use preg_replace
来源:https://stackoverflow.com/questions/11972695/why-is-this-regex-str-replace-not-working