【发布时间】:2018-02-26 12:44:43
【问题描述】:
我有一组这样的字符串:
$string1 = 'man_city/man_united'; //it will be replaced
$string2 = 'liverpool///arsenal'; //it will not be replaced
$string3 = 'chelsea//spurs'; //it will not be replaced
$string4 = 'leicester/sunderland'; //it will be replaced
我想用'/'替换字符串中的'/'字符,但前提是'/'字符中的下一个或上一个字符也不包含'/'。
如果我像这样使用 str_replace,它将不起作用:
$name1 = str_replace("/","\/",$string1);
$name2 = str_replace("/","\/",$string2);
...
//output
$name1 = 'man_city\/man_united';
$name2 = 'liverpool\/\/\/arsenal';
...
//desired output
$name1 = 'man_city\/man_united';
$name2 = 'liverpool///arsenal';
...
【问题讨论】:
-
放上想要输出的例子。