Why is this regex str_replace not working? [closed]

萝らか妹 提交于 2021-02-05 09:23:45

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!