【发布时间】:2016-02-02 05:46:12
【问题描述】:
如何将字符串中的双反斜杠组替换为*
例如:
\\\\a -> **a
\\\a -> *\a
\\a -> *a
\a -> \a
我尝试同时使用str_replace 和preg_replace,但在第二种情况下失败了
str_replace('\\', '*', $str);
preg_replace('/\\\\/', '*', $str);
=> `\\\a` -> `**a`
我想这样做的原因是我想要与Split string by delimiter, but not if it is escaped 相同的东西,但是
$str = '1|2\|2|3\\|4\\\|4';
$r = preg_split('~\\\\.(*SKIP)(*FAIL)|\|~s', $str);
var_dump($r);
给我。
0 => string '1' (length=1)
1 => string '2\|2' (length=4)
2 => string '3\|4\\' (length=6)
3 => string '4' (length=1)
我期待类似的东西
[0] => 1
[1] => 2\|2
[2] => 3\\
[3] => 4\\\|4
php 5.4.45-2
【问题讨论】: