Home > Article > Backend Development > How to remove blank lines in php
php去除空行的方法:首先通过“preg_replace("/(\r\n|\n|\r|\t)/i", '', $text);”语句去除多余的空格或换行;然后对Html里有连续的空行或tab给正则过滤掉即可。
推荐:《PHP视频教程》
php:去掉多余的空行
<?php $str="i am a book\n\n\n\n\nmoth"; //去除所有的空格和换行符 echo preg_replace("/[\s]{2,}/","",$str).'<br>'; //去除多余的空格和换行符,只保留一个 echo preg_replace("/([\s]{2,})/","\\1",$str); //去除多余的空格或换行 $text = preg_replace("/(\r\n|\n|\r|\t)/i", '', $text); $lastSeveralLineContentsArr = preg_replace("/([ |\t]{0,}[\n]{1,}){2,}/","",$lastSeveralLineContentsArr); //对Html里有连续的空行或tab给正则过滤掉> ?>
The above is the detailed content of How to remove blank lines in php. For more information, please follow other related articles on the PHP Chinese website!