php實現(xiàn)轉(zhuǎn)換html格式為文本格式的方法
本文實例講述了php實現(xiàn)轉(zhuǎn)換html格式為文本格式的方法。分享給大家供大家參考,具體如下:
有時候需要轉(zhuǎn)換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
對于代碼及編碼轉(zhuǎn)換感興趣的朋友還可參考本站在線工具:
在線編碼轉(zhuǎn)換工具(utf-8/utf-32/Punycode/Base64):
http://tools.jb51.net/transcoding/decode_encode_tool
在線UBB/HTML代碼轉(zhuǎn)換工具:
http://tools.jb51.net/transcoding/ubb2html
中文繁體字簡體字轉(zhuǎn)換(繁簡轉(zhuǎn)換)工具:
http://tools.jb51.net/transcoding/convertzh
在線圖片轉(zhuǎn)換BASE64工具:
http://tools.jb51.net/transcoding/img2base64
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)組(Array)操作技巧大全》、《php排序算法總結(jié)》、《PHP常用遍歷算法與技巧總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設計算法總結(jié)》、《PHP數(shù)學運算技巧總結(jié)》、《php正則表達式用法總結(jié)》、《PHP運算與運算符用法總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關(guān)文章
PHP 雜談《重構(gòu)-改善既有代碼的設計》之三 重新組織數(shù)據(jù)
承接上文的PHP 雜談《重構(gòu)-改善既有代碼的設計》之 重新組織你的函數(shù)繼續(xù)重構(gòu)方面的內(nèi)容2012-04-04php中替換字符串函數(shù)strtr()和str_repalce()的用法與區(qū)別
在php中替換函數(shù)主要有strtr(),str_repalce()這兩個函數(shù),下面這篇文中主要給大家介紹下這兩者之間的區(qū)別和用法,文中通過示例代碼介紹的很詳細,有需要的朋友們可以參考借鑒,下面跟著小編一起來學習學習吧。2016-11-11fleaphp rolesNameField bug解決方法
fleaphp rolesNameField bug解決方法,需要的朋友可以參考下。2011-04-04PHP中include()與require()的區(qū)別說明
本文主要介紹了PHP中include()與require()的區(qū)別。具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02