如何删除所有标签内的空格,但是,这很重要,不删除\n
在这个例子中,我有两种类型的标签。我正在寻找可以在具有任何类型标签的任何文本中使用的东西。
我有这个:
$text ="<p> some text </p><h2> some text</h2>\n
<p>some text </p><h2>some text </h2>";
我想要这个:
<p>some text</p><h2>some text</h2>\n
<p>some text</p><h2>some text</h2>
我试过:
$text = preg_replace ("/>\s+/", ">", $text);//remove space from start
$text = preg_replace ("/\s+</", "<", $text);//and end
echo $text;
问题是,它也删除了\n。
最佳答案
您已关闭,请尝试:
$text = preg_replace ("/> +/", ">", $text);//remove space from start
$text = preg_replace ("/ +</", "<", $text);//and end
如果您还想删除表格:
$text = preg_replace ("/>\h+/", ">", $text);//remove space from start
$text = preg_replace ("/\h+</", "<", $text);//and end
\h
代表水平空间。
关于php - 删除标签内的空白而不删除\n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35100694/