how to replace group of double backslash in a string to *
for example:
\\\\a -> **a
\\\a -> *\a
\\a -> *a
\a -> \a
I try use both str_replace
and preg_replace
but it failed at the second case
str_replace('\\', '*', $str);
preg_replace('/\\\\/', '*', $str);
=> `\\\a` -> `**a`
the reason i want to do this is i want something same as Split string by delimiter, but not if it is escaped but
$str = '1|2\|2|3\\|4\\\|4';
$r = preg_split('~\\\\.(*SKIP)(*FAIL)|\|~s', $str);
var_dump($r);
give me.
0 => string '1' (length=1)
1 => string '2\|2' (length=4)
2 => string '3\|4\\' (length=6)
3 => string '4' (length=1)
i expect something like
[0] => 1
[1] => 2\|2
[2] => 3\\
[3] => 4\\\|4
php 5.4.45-2