Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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 “如何替换”;匹配“U表达式\n”;用preg_替换还是其他?_Php_Regex_Preg Replace - Fatal编程技术网

Php “如何替换”;匹配“U表达式\n”;用preg_替换还是其他?

Php “如何替换”;匹配“U表达式\n”;用preg_替换还是其他?,php,regex,preg-replace,Php,Regex,Preg Replace,我想知道如何将这些匹配项以字符串形式替换为[/img]\n(\n:新行) 我尝试了以下每一项 $string = preg_replace("#[/img]\n#","[/img]",$string); $string = preg_replace("/\[\/img\]\\n/","[/img]",$string); $string = preg_replace("/\[\/img\]\n/si","[/img]",$string); 但什么都不管用 $string = preg_repla

我想知道如何将这些匹配项以字符串形式替换为
[/img]\n
(\n:新行)

我尝试了以下每一项

$string = preg_replace("#[/img]\n#","[/img]",$string);
$string = preg_replace("/\[\/img\]\\n/","[/img]",$string);
$string = preg_replace("/\[\/img\]\n/si","[/img]",$string);
但什么都不管用

$string = preg_replace("\n","Test",$string);
无需
[/img]
搜索即可正常工作

我不明白为什么我不能让它工作

示例

[img=y40h3inre6]Stock options[/img]

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
应成为以下内容:

[img=y40h3inre6]Stock options[/img]
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
你可以试一下

输出 您是否尝试:

$string = preg_replace('~\[/img]\s++~', "[/img]\n", $string);
还是这个

$string = preg_replace('~\[/img]\K\s++~', "\n", $string);
\n
表示换行符,但如果要匹配字符串
'\n'
,则必须在模式中使用
'\\\n'


请注意,Rikesh,某些系统上的新行可能不同(\n\r\n\n\r)

谢谢您的回答。我编辑了我的问题
\n
=换行符,而不是字符串。不幸的是,你的解决方案没有从
[/img]
@Valky中删除
\n
:我已经编辑了我的文章,因为你展示了一个你想要的例子。很好,你找到了它(第一个,只有
“[/img]”
)!对于一个小问题来说,这是一个多么壮观的正则表达式。。。非常感谢。@Valky:你可以这样做:
~\s++(?=\[img\b)~=>“\n”
~\s++\[img\b~=>”\n[img”
我忘了告诉你(.*)。所以我用
'~\s++\[img=(.*?),“[img=$1]”来完成它。谢谢你的帮助。
$string = preg_replace('~\[/img]\s++~', "[/img]\n", $string);
$string = preg_replace('~\[/img]\K\s++~', "\n", $string);