實用的php清除html,php去除空格與換行,php清除空白行和換行,提取頁面純文本


實用的php清除html,換行,空格類,php去除空格與換行,php清除空白行和換行,提取頁面純文本內容

方法一:

function DeleteHtml($str) 
{ 
$str = trim($str); //清除字符串兩邊的空格
$str = preg_replace("/\t/","",$str); //使用正則表達式替換內容,如:空格,換行,並將替換為空。
$str = preg_replace("/\r\n/","",$str); 
$str = preg_replace("/\r/","",$str); 
$str = preg_replace("/\n/","",$str); 
$str = preg_replace("/ /","",$str);
$str = preg_replace("/  /","",$str);  //匹配html中的空格
return trim($str); //返回字符串
}

調用方法

DeleteHtml($str);   $str 為需要清除的頁面字符串

方法二:
strip_tags() 函數剝去 HTML、XML 以及 PHP 的標簽。
strip_tags() 函數官方介紹: http://www.php.net/manual/zh/function.strip-tags.php

class delhtml{
public function DeleteHtml($str) 
{ 
$str = trim($str); //清除字符串兩邊的空格
$str = strip_tags($str,""); //利用php自帶的函數清除html格式
$str = preg_replace("/\t/","",$str); //使用正則表達式替換內容,如:空格,換行,並將替換為空。 $str = preg_replace("/\r\n/","",$str); $str = preg_replace("/\r/","",$str); $str = preg_replace("/\n/","",$str); $str = preg_replace("/ /","",$str); $str = preg_replace("/  /","",$str); //匹配html中的空格 return trim($str); //返回字符串 } }

調用方法

$delhtml=new delhtml();
$doreplace=$delhtml->DeleteHtml($doreplace);//需要處理的頁面字符串

方法三:
去除字符串內部的空行:

$str = preg_replace("/(s*?r?ns*?)+/","n",$str);

去除全部的空行,包括內部和頭尾:

$str = preg_replace('/($s*$)|(^s*^)/m', '',$str);

站長總結:三種方式方法過濾清除頁面中的換行及空白行、空白換行,都有相同的效果,大家看實際使用情況吧。

 

轉:http://www.adocode.com/code/238


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM