PHP implements a method of converting HTML format to text format _php tips

Source: Internet
Author: User
Tags base64 php programming php regular expression

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.

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.