php如何去除html,空格,換行,提取純文字

coldplay.xixi
發布: 2023-03-04 15:04:01
原創
2485 人瀏覽過

php去除html,空格,換行,提取純文字的方法:1、清除字串兩邊的空格,程式碼為【$str = trim($str)】;2、匹配html中的空格,程式碼為【$str = preg_replace("/  /","",$str)】。

php如何去除html,空格,換行,提取純文字

php移除html,空格,換行,提取純文字的方法:

方法一:

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 為需要清除的頁面字符字串

方法二:

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); //返回字符串
}
登入後複製

方法三:

去移除字串內部的空白行:

$str = preg_replace("/(s*?r?ns*?)+/","n",$str);
登入後複製

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

$str = preg_replace('/($s*$)|(^s*^)/m', '',$str);
登入後複製

相關學習推薦:php程式設計(影片)

#

以上是php如何去除html,空格,換行,提取純文字的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!