删除HTML中标记之间的空白 [英] Remove white spaces between tags in HTML

查看:87
本文介绍了删除HTML中标记之间的空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码去除html中的空格。我只想删除两个标签之间的空格。但是下面的代码会替换所有的空格



IE删除>和<之间的所有空格

  //读取整个字符串
$ str = file_get_contents('sample.txt');

//替换所有空格
$ str = str_replace(\\\
,,$ str);
$ str = str_replace(\t,,$ str);
$ str = str_replace(,,$ str);

//写入整个字符串
file_put_contents('sample.txt',$ str);


解决方案

您需要使用常规表达式。



也许你可以使用这个:

$ $ p $ $ preg_replace('/(\\ \s *(\<)/ m','$ 1 $ 2',$ html);


I am using the following code to remove white spaces in html. I only want to remove white spaces in betweens tags. But below code replaces all white spaces

I.E remove all white spaces in between ">" and "<"

//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);

解决方案

You need use a regular expresion.

Maybe you can use this:

$html = preg_replace('/(\>)\s*(\<)/m', '$1$2', $html);

这篇关于删除HTML中标记之间的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆