This article describes the PHP implementation of the conversion of HTML format text Format method. Share to everyone for your reference, specific as follows:
There are times when you need to convert HTML-formatted strings to text, but you need to keep a certain format, such as a paragraph format that requires paragraphs to be formatted to use the following function
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: Small knitting here recommend a site for the layout of the PHP format landscaping tools to help you in the future of PHP programming code layout:
PHP Code online Format Landscaping tool:Http://tools.jb51.net/code/phpformat
For code and coding conversion interested friends can also refer to the site online tools:
Online Coding 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
Simplified Chinese characters conversion (simplified conversion) tool:
Http://tools.jb51.net/transcoding/convertzh
Online Image conversion BASE64 tool:
Http://tools.jb51.net/transcoding/img2base64
More about PHP Interested readers can view the site topics: "PHP array" Operation tips Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", " PHP Mathematical Calculation Skills Summary, "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"
I hope this article will help you with the PHP program design.