Really simple question: how can I preg_replace
the backslash character?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Yes, but you need to escape it. When using it in the regexp use \\
to use it in the replacement, use \\\\
(that will turn into \\
that will be interpreted as a single backslash).
回答2:
You need to escape the backslash: \\
From the manual on preg_replace
:
To use backslash in replacement, it must be doubled (
"\\\\"
PHP string).
Alternatively, use preg_quote
to prepare a string for a preg_*
operation.
回答3:
You could try
$a = "\\\\";
$a = preg_replace('/\\\\/','/',$a);
Output:
'//'
回答4:
Escape \
with \
: \\
preg_replace('/\\/', 'REMOVED BACKSLASH', 'sometest\othertest');
回答5:
Use it twice eg \\
回答6:
This code works for me
$text = "replace \ backslash";
$rep = "";
$replace_text = preg_replace( '/\\\\{1}/',$rep,$text);
echo $replace_text;
Output :
replace backslash