Php converts html format to text format,
This document describes how to convert html to text in php. We will share this with you for your reference. The details are as follows:
Sometimes you need to convert the html format string to text, but you need to maintain a certain format. For example, you can use the following function to convert a paragraph into a segment format.
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: I recommend a php formatting and formatting typographical tool on this site to help you typeset code in future PHP programming:
Php code online formatting and beautification tools:Http://tools.jb51.net/code/phpformat
If you are interested in code and code conversion, refer to the online tools on this site:
Online encoding conversion tool (UTF-8/utf-32/Punycode/Base64 ):
Http://tools.jb51.net/transcoding/decode_encode_tool
Online UBB/HTML code conversion tool:
Http://tools.jb51.net/transcoding/ubb2html
Conversion Tool for simplified Chinese characters (Simplified Chinese characters:
Http://tools.jb51.net/transcoding/convertzh
Online image conversion BASE64 tool:
Http://tools.jb51.net/transcoding/img2base64