preg_replace() [function.preg-replace]: Unknown modifier '/' in /home/

时间秒杀一切 提交于 2019-12-02 20:57:50

问题


in my website i want to replace links with some other link like this

www.abc.com or http://abc.com will be replaced with

http://www.XXXXXX.com/sonal?www.abc.com

or

http://www.XXXXXX.com/sonal?http://abc.com

so i am using this code but this code is giving some error

Warning: preg_replace() [function.preg-replace]: Unknown modifier '/' in /home/XXXXX/public_html/YYYYYYYYY/KKKKKK.php on line 495

$search_array = array(
   "/\[url]www|http://.([^'\"]*)\[\/url]/iU",
   "/\[url]([^'\"]*)\[\/url]/iU",
   "/\[url=www|http://.([^'\"\s]*)](.*)\[\/url]/iU",
   "/\[url=([^'\"\s]*)](.*)\[\/url]/iU"
);
$replace_array = array(
   "<a href=\"http://XXXXXX.com/sonal?.\\1\" target=\"_blank\" rel=\"nofollow\">www.   \\1</a>",
   "<a href=\"\\1\" target=\"_blank\" rel=\"nofollow\">\\1</a>",
   "<a href=\"http://XXXXXX.com/sonal?.\\1\" target=\"_blank\" rel=\"nofollow\">\\2</a>",
   "<a href=\"\\1\" target=\"_blank\" rel=\"nofollow\">\\2</a>"
);

$msg = preg_replace($search_array, $replace_array, $msg);
return $msg;

回答1:


There is few ways to solve that

Escaping "/"

$search_array = array(
  "/\[url]www|http:\/\/.([^'\"]*)\[\/url]/iU",
  "/\[url]([^'\"]*)\[\/url]/iU",
  "/\[url=www|http:\/\/.([^'\"\s]*)](.*)\[\/url]/iU",
  "/\[url=([^'\"\s]*)](.*)\[\/url]/iU"
);

Or using different regexp seperator like "#"

$search_array = array(
  "#\[url]www|http://.([^'\"]*)\[\/url]#iU",
  "#\[url]([^'\"]*)\[\/url]#iU",
  "#\[url=www|http://.([^'\"\s]*)](.*)\[\/url]#iU",
  "#\[url=([^'\"\s]*)](.*)\[\/url]#iU"
);



回答2:


"/\[url]www|http://.([^'\"]*)\[\/url]/iU",
 ^               ^^                  ^

You either need to escape the two // in the middle to \/\/, or, better, use different delimiters for the regex:

"~\[url]www|http://.([^'\"]*)\[/url]~iU",


来源:https://stackoverflow.com/questions/10395349/preg-replace-function-preg-replace-unknown-modifier-in-home

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