Php 删除'的第一个实例\n';绕着绳子

Php 删除'的第一个实例\n';绕着绳子,php,regex,Php,Regex,嗨,我有以下情况,我有标签包装在我的内容和以下输入 [hello] Text here [/hello] [hello] Text here 2 [/hello] 输出是 [hello]\n\nText here\n\n[/hello]\n\n[hello]\n\nText here 2\n\n[/hello] 所需的输出应为 [hello]\nText here\n[/hello][hello]\nText here 2\n[/hello] PS:谢谢您的回答,但是由于它的用户

嗨,我有以下情况,我有标签包装在我的内容和以下输入

[hello]

Text here

[/hello]

[hello]

Text here 2

[/hello]
输出是

[hello]\n\nText here\n\n[/hello]\n\n[hello]\n\nText here 2\n\n[/hello]
所需的输出应为

[hello]\nText here\n[/hello][hello]\nText here 2\n[/hello]
PS:谢谢您的回答,但是由于它的用户输入,所以有可能间隔[hello]\n

是否有一种使用php的方法来修剪开始标记([hello])和结束标记([/hello])周围的第一个\n标记?谢谢

很简单,试试这个:

$str = "[hello]\n\nText here\n\n[/hello]\n\n[hello]\n\nText here 2\n\n[/hello]";
echo str_replace("[hello]\n","[hello]", str_replace("\n[/hello]","[/hello]",$str));

这很简单,换句话说,您希望将
\n\n
替换为
\n

$str = "[hello]\n\nText here\n\n[/hello]\n\n[hello]\n\nText here 2\n\n[/hello]";
echo str_replace("\n\n", "\n", $str);
它将输出

[hello]
Text here
[/hello]
[hello]
Text here 2
[/hello]

我通常使用
“\n”.trim($string)。“\n”
我不明白。请提供所需的输出。谢谢Anne,但在这种情况下,即使输出有一个\nHi,如果有空格,它也会添加\n,例如。[你好]\n是否仍会删除\n?否,如果您不确定它是否包含空格,请尝试将
\n\n
替换为
\n