小编seg*_*gon的帖子

删除标签内的空白而不删除 \n

如何删除所有标签内的空格,但是,这很重要,不删除 \n

在这个例子中,我有两种类型的标签。我正在寻找可以在任何带有任何类型标签的文本中使用的东西。

我有这个:

$text ="<p> some text </p><h2> some text</h2>\n
<p>some text </p><h2>some text </h2>";
Run Code Online (Sandbox Code Playgroud)

我要这个:

<p>some text</p><h2>some text</h2>\n 
<p>some text</p><h2>some text</h2>
Run Code Online (Sandbox Code Playgroud)

我试过:

$text = preg_replace ("/>\s+/", ">", $text);//remove space from start
$text = preg_replace ("/\s+</", "<", $text);//and end
echo $text;
Run Code Online (Sandbox Code Playgroud)

问题是,它也删除了 \n。

php regex

3
推荐指数
1
解决办法
1091
查看次数

标签 统计

php ×1

regex ×1