Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_replace替换包含;[“和”和“];_Php_Preg Replace - Fatal编程技术网

Php preg_replace替换包含;[“和”和“];

Php preg_replace替换包含;[“和”和“];,php,preg-replace,Php,Preg Replace,如何编写转换为以下内容的preg_replace字符串: "[[ STRING1 | STRING 2 ]]" 到 在PHP中?我在匹配保留的字符“[”、“]”和“|”时遇到问题。在符号前使用\对其进行转义:\[,\]和\在符号前使用\对其进行转义:\[/code>,]和\\\\\\只需在regexp中转义它们:“[”=>“\[”只需在regexp中转义它们:“[”“\[”preg\u replace(“~\[\[(.+)\[(.+)\.+)\]\]\]\]~iUs',”,“$string

如何编写转换为以下内容的preg_replace字符串:

"[[ STRING1 | STRING 2 ]]"



在PHP中?我在匹配保留的字符“[”、“]”和“|”时遇到问题。

在符号前使用
\
对其进行转义:
\[
\]
\

在符号前使用
\
对其进行转义:
\[/code>,
]
\\\\\\

只需在regexp中转义它们:
“[”
=>
“\[”
只需在regexp中转义它们:
“[”
“\[”

preg\u replace(“~\[\[(.+)\[(.+)\.+)\]\]\]\]~iUs',”,“$string”);
preg\u replace(“~\[\[(.+)\\\\\\.(.+)\]\]\]~iUs',”$string);

正如其他人已经说过的,您必须使用
\
转义任何特殊字符。您还可以使用转义传递给正则表达式的文本(如果您正在构建dinamic regexp,则非常有用)

正如其他人所说,您必须使用
\
转义任何特殊字符。您还可以使用转义传递给正则表达式的文本(如果您正在构建dinamic regexp,则非常有用)

arg太晚了,快!到时间机器!arg太晚了,快!到时间机器!
<a href='STRING 2'>STRING1</a>
preg_replace('~\[\[(.+)\|(.+)\]\]~iUs','<a href="$2">$1</a>',$string);