This article mainly introduces the PHP implementation to convert HTML format to text Format method, through a custom function to implement the filtering for HTML tags, involving the PHP regular replacement of the relevant operational skills, the need for friends can refer to the next
Sometimes you need to convert the HTML format string to text, but you need to maintain a certain format, such as the paragraph required to change the format 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 ("/<\\/?p>/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;}
Summary: The above is the entire content of this article, I hope to be able to help you learn.