删除字符串周围的第一个'\ n'实例

时间:2011-10-29 08:44:29

标签: 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] \ n

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

2 个答案:

答案 0 :(得分:1)

这很简单,换句话说,您要将\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]

答案 1 :(得分:0)

简单,试试这个:

$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));