php實現(xiàn)轉換html格式為文本格式的方法
本文實例講述了php實現(xiàn)轉換html格式為文本格式的方法。分享給大家供大家參考,具體如下:
有時候需要轉換html格式的字符串為文本,但又需要保持一定的格式,比如要求段落變成的分段格式就可以用下面這個函數(shù)
function html2text($str){
$str = preg_replace("/<style .*?<\\/style>/is", "", $str);
$str = preg_replace("/<script .*?<\\/script>/is", "", $str);
$str = preg_replace("/<br \\s*\\/>/i", ">>>>", $str);
$str = preg_replace("/<\\/?p>/i", ">>>>", $str);
$str = preg_replace("/<\\/?td>/i", "", $str);
$str = preg_replace("/<\\/?div>/i", ">>>>", $str);
$str = preg_replace("/<\\/?blockquote>/i", "", $str);
$str = preg_replace("/<\\/?li>/i", ">>>>", $str);
$str = preg_replace("/ /i", " ", $str);
$str = preg_replace("/ /i", " ", $str);
$str = preg_replace("/&/i", "&", $str);
$str = preg_replace("/&/i", "&", $str);
$str = preg_replace("/</i", "<", $str);
$str = preg_replace("/</i", "<", $str);
$str = preg_replace("/“/i", '"', $str);
$str = preg_replace("/&ldquo/i", '"', $str);
$str = preg_replace("/‘/i", "'", $str);
$str = preg_replace("/&lsquo/i", "'", $str);
$str = preg_replace("/'/i", "'", $str);
$str = preg_replace("/&rsquo/i", "'", $str);
$str = preg_replace("/>/i", ">", $str);
$str = preg_replace("/>/i", ">", $str);
$str = preg_replace("/”/i", '"', $str);
$str = preg_replace("/&rdquo/i", '"', $str);
$str = strip_tags($str);
$str = html_entity_decode($str, ENT_QUOTES, "utf-8");
$str = preg_replace("/&#.*?;/i", "", $str);
return $str;
}
PS:小編在這里推薦一款本站的php格式化美化的排版工具幫助大家在以后的PHP程序設計中進行代碼排版:
php代碼在線格式化美化工具:http://tools.jb51.net/code/phpformat
對于代碼及編碼轉換感興趣的朋友還可參考本站在線工具:
在線編碼轉換工具(utf-8/utf-32/Punycode/Base64):
http://tools.jb51.net/transcoding/decode_encode_tool
在線UBB/HTML代碼轉換工具:
http://tools.jb51.net/transcoding/ubb2html
中文繁體字簡體字轉換(繁簡轉換)工具:
http://tools.jb51.net/transcoding/convertzh
在線圖片轉換BASE64工具:
http://tools.jb51.net/transcoding/img2base64
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP數(shù)組(Array)操作技巧大全》、《php排序算法總結》、《PHP常用遍歷算法與技巧總結》、《PHP數(shù)據(jù)結構與算法教程》、《php程序設計算法總結》、《PHP數(shù)學運算技巧總結》、《php正則表達式用法總結》、《PHP運算與運算符用法總結》、《php字符串(string)用法總結》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
php通過array_merge()函數(shù)合并關聯(lián)和非關聯(lián)數(shù)組的方法
這篇文章主要介紹了php通過array_merge()函數(shù)合并關聯(lián)和非關聯(lián)數(shù)組的方法,涉及php中array_merge()函數(shù)操作數(shù)組合并的技巧,需要的朋友可以參考下2015-03-03
PHP中include()與require()的區(qū)別說明
本文主要介紹了PHP中include()與require()的區(qū)別。具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02
非集成環(huán)境的php運行環(huán)境(Apache配置、Mysql)搭建安裝圖文教程
這篇文章主要介紹了非集成環(huán)境的php運行環(huán)境(Apache配置、Mysql)搭建安裝圖文教程,感興趣的小伙伴們可以參考一下2016-04-04

