开发者

PHP preg_replace \

Really simple question: how can I preg_replace the backslas开发者_运维技巧h character?


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).


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.


You could try

$a = "\\\\";
$a = preg_replace('/\\\\/','/',$a);

Output:

'//'


This code works for me

  $text = "replace \ backslash";
  $rep = "";
  $replace_text = preg_replace( '/\\\\{1}/',$rep,$text);
  echo $replace_text;

Output :

replace backslash


Escape \ with \: \\

preg_replace('/\\/', 'REMOVED BACKSLASH', 'sometest\othertest');


Use it twice eg \\


If you want to remove backslash from text and don't want to see it anymore. then use this php function. But if it is a double backslash it will only remove one. stripslashes ($string)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜