php去除html,空格,換行,提取純文字的方法:1、清除字符串兩邊的空格,代碼為【$str = trim($str)】;2、匹配html中的空格,代碼為【$str = preg_replace("/ /","",$str)】。
php去除html,空格,換行,提取純文字的方法:
方法一:
function DeleteHtml($str) { $str = trim($str); //清除字符串兩邊的空格 $str = preg_replace("/t/","",$str); //使用正則表達(dá)式替換內(nèi)容,如:空格,換行,并將替換為空。 $str = preg_replace("/rn/","",$str); $str = preg_replace("/r/","",$str); $str = preg_replace("/n/","",$str); $str = preg_replace("/ /","",$str); $str = preg_replace("/ /","",$str); //匹配html中的空格 return trim($str); //返回字符串 }
調(diào)用方法
DeleteHtml($str);
$str
為需要清除的頁(yè)面字符串
方法二:
function DeleteHtml($str) { $str = trim($str); //清除字符串兩邊的空格 $str = strip_tags($str,""); //利用php自帶的函數(shù)清除html格式 $str = preg_replace("/t/","",$str); //使用正則表達(dá)式替換內(nèi)容,如:空格,換行,并將替換為空。 $str = preg_replace("/rn/","",$str); $str = preg_replace("/r/","",$str); $str = preg_replace("/n/","",$str); $str = preg_replace("/ /","",$str); $str = preg_replace("/ /","",$str); //匹配html中的空格 return trim($str); //返回字符串 }
方法三:
去除字符串內(nèi)部的空行:
$str = preg_replace("/(s*?r?ns*?)+/","n",$str);
去除全部的空行,包括內(nèi)部和頭尾:
$str = preg_replace('/($s*$)|(^s*^)/m', '',$str);
相關(guān)學(xué)習(xí)推薦:php編程(視頻)