How PHP converts HTML format to text format

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.