文章摘要
这篇文章介绍了如何使用PHP处理HTML文本中的空格和换行符。代码展示了通过`str_replace`和`preg_replace`函数来替换或去除各种类型的空格(包括换行符、制表符和普通空格),以确保文本干净。例如,通过`preg_replace('/s+/','',$html);`来去除连续的空格,通过`preg_replace('/[nrt]/','',$html);`来去除换行符和制表符。此外,代码还提到了使用`trim()`函数来去除字符串两端的空白。这种方法适用于处理包含空格的HTML文本,以确保文本的一致性和可读性。
$html=”<p>fdasf</p>”;
echo $string=str_replace(array(“<p>”,””,”</p>”),””,$html);
br<http://bbs.houdunwang.com/>
若是<p> 内容</p>替换成<p>内容</p><p> content</p>替换成<p>contend</p>(空格是tab键和空格键 混合的 都有可能)方法如下
$html=preg_replace(‘/[\n\r\t]/’,”,$html);//去空格
若是<p>后面跟了若干个,再是内容
<p> 内容</p>
替换成<p>内容</p>
<p> content</p>
替换成<p>contend</p>
<?php
$html=”<p>
内容</p>替换成<p>内容</p>
<p>content</p>替换成<p>contend</p>”;方法如下
$html=trim($html);
$html=str_replace(PHP_EOL,””,$html);
$html=str_replace(” “,””,$html);
$html=preg_replace(‘/\s+/’,”,$html);
$html=preg_replace(‘/[\n\r\t]/’,”,$html);
echo “{$html}”;
?>
str_replace(“<p><br/></p>”,””,$htmlstr);
$html=”<p>fdasf</p>”;
echo $string=str_replace(array(“<p>”,””,”</p>”),””,$html);
br<http://bbs.houdunwang.com/>
若是<p> 内容</p>替换成<p>内容</p><p> content</p>替换成<p>contend</p>(空格是tab键和空格键 混合的 都有可能)方法如下
$html=preg_replace(‘/[\n\r\t]/’,”,$html);//去空格
若是<p>后面跟了若干个,再是内容
<p> 内容</p>
替换成<p>内容</p>
<p> content</p>
替换成<p>contend</p>
<?php
$html=”<p>
内容</p>替换成<p>内容</p>
<p>content</p>替换成<p>contend</p>”;方法如下
$html=trim($html);
$html=str_replace(PHP_EOL,””,$html);
$html=str_replace(” “,””,$html);
$html=preg_replace(‘/\s+/’,”,$html);
$html=preg_replace(‘/[\n\r\t]/’,”,$html);
echo “{$html}”;
?>
str_replace(“<p><br/></p>”,””,$htmlstr);
© 版权声明
文章版权归作者所有,未经允许请勿转载。



