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

标签 php regex

如何删除所有标签内的空格,但是,这很重要,不删除\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/

相关文章:

java - 两个字符之间的字符串的正则表达式

python - 将 YouTube 链接转换为嵌入链接

Java正则表达式删除字符串的中间

php - 全文 bool 模式和mysql中的like之间的区别?

php - 查询解析不对,怎么办?

regex - 在 tcl 中使用正则表达式识别换行符

Java 正则表达式行为

php - codeigniter 代码中的 Ajax 自动搜索不起作用

javascript - jQuery - 将标签和复选框组限制为最大数量

php - 可排序项目 MySQL 结构