php實現轉換html格式為文字格式設定的方法_php技巧

來源:互聯網
上載者:User

本文執行個體講述了php實現轉換html格式為文字格式設定的方法。分享給大家供大家參考,具體如下:

有時候需要轉換html格式的字串為文本,但又需要保持一定的格式,比如要求段落變成的分段格式就可以用下面這個函數

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數組(Array)操作技巧大全》、《php排序演算法總結》、《PHP常用遍曆演算法與技巧總結》、《PHP資料結構與演算法教程》、《php程式設計演算法總結》、《PHP數學運算技巧總結》、《phpRegex用法總結》、《PHP運算與運算子用法總結》、《php字串(string)用法總結》及《php常見資料庫操作技巧匯總》

希望本文所述對大家PHP程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.