如何删除所有标签内的空格,但是,这很重要,不删除 \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。
您已关闭,请尝试使用:
$text = preg_replace ("/> +/", ">", $text);//remove space from start
$text = preg_replace ("/ +</", "<", $text);//and end
Run Code Online (Sandbox Code Playgroud)
如果您还想删除表格:
$text = preg_replace ("/>\h+/", ">", $text);//remove space from start
$text = preg_replace ("/\h+</", "<", $text);//and end
Run Code Online (Sandbox Code Playgroud)
\h 代表水平空间。