php - 删除 HTML 中标签之间的空格

标签 php html regex domdocument

我正在使用以下代码删除 html 中的空格。我只想删除标签之间的空格。但是下面的代码替换了所有空格

I.E 删除“>”和“<”之间的所有空格

//read the entire string
$str=file_get_contents('sample.txt');

//replace all white spaces
$str=str_replace("\n", "",$str);
$str=str_replace("\t", "",$str);
$str=str_replace(" ", "",$str);

//write the entire string
file_put_contents('sample.txt', $str);

最佳答案

您需要使用正则表达式。

也许你可以用这个:

$html = preg_replace('/\>\s+\</m', '><', $html);

在这里测试https://repl.it/

关于php - 删除 HTML 中标签之间的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36096319/

相关文章:

php - for 循环与 isset() != null - 为什么?

php - Laravel-5 每个日志表的独立模型

正则表达式匹配任意字符串中的单个非连续制表符?

javascript - 除非信息填写正确,否则如何使提交按钮不可点击?

php - 使用正则表达式从网站的 HTML 源代码中提取内容

php - 是否可以使用php将foxpro数据导出到excel

php - 阅读受密码保护的页面

javascript - JQuery/Javascript 选择器使用 toggle() 或 add/removeClass() 遍历元素数组

javascript - 使用 KaTeX 时,为什么在头部放一个 &lt;script&gt;,在主体后放一个?

javascript - Internet Explorer 9 及以下版本,在主体上使用 rtl 方向时将滚动条设置为右侧