Example of PHP converting HTML into plain text content

Source: Internet
Author: User
Tags mail example
We can use many methods to convert html to plain text. However, the simplest method is to use the strip_tags function. However, some friends may find that they can filter out html by using a user-defined function. The following describes some methods. convert HTML to plain text: sometimes you may need... we can use many methods to convert html to plain text. However, the simplest method is to use the strip_tags function. However, some friends may find that they can filter out html by using a user-defined function. The following describes some methods.

Convert HTML to plain text: sometimes you may need to convert HTML to plain text. you can use the strip_tags () function to delete all HTML and PHP tags in the string, only text entities are left, in the form:

String strip_tags (string str [, string allowable_tags])

The optional allowable_tags parameter specifies the tag that can be skipped during this process. The following example uses strip_tags () to delete the HTML tag from the string. the code is as follows:

$ Input = "Email example@example.com"; echo strip_tags ($ input); This returns the following result: the example below in the e-mail example@example.com deletes all the tags outside of the tag: $ input = "This example is yanshare! "; Echo strip_tags ($ input," "); // The Returned result is as follows: This example is yanshare! In PHP
The code is as follows: function br2nl ($ text) {return preg_replace ('/ /I ', '', $ text);} // Or: function br2nl ($ text) {$ text = preg_replace ('/ /I ', chr (13), $ text); return preg_replace (' // I ', '', $ text );}

The code is as follows:

 ] *. *? Script 'Si ", // remove javascript" '<[/!] *? [^ <>] * 'Si ", // remove the HTML tag" '([rn]) [s] + '", // remove the blank character "'& (quot | #34);' I", // replace the HTML entity "'& (amp | #38);' I ", "'& (lt | #60);' I", "'& (gt | #62);' I", "'& (nbsp | #160 ); 'I ","' & (iexcl | #161); 'I ","' & (cent | #162); 'I ", "'& (pound | #163);' I", "'& (copy | #169);' I", "'& # (d + ); 'E "); // run $ replace = array (" "," "," 1 "," \ "", "&", "<", "> ","",

Chr (1, 161 ),

Chr (1, 162 ),

Chr (1, 163 ),

Chr (1, 169 ),

"Chr (1 )");

$ Text = preg_replace ($ search, $ replace, $ document );

 

// Here, I have converted my HTML to TXT text. haha, this function is really convenient.

// The following are some of the plug-in's word splitting operations.

Later, I saw a method written using PHP on the internet. using this method can also convert HTML into TXT text. I personally think it is quite practical. here I will share it with you, the code is as follows:

Function HtmlToText ($ str) {$ str = preg_replace ("/
  
   
|
   
    
|
    /IsU "," ", $ str); // remove CSS styles, JS scripts, HTML comments $ alltext = ""; // The variable $ start = 1 used to save the TXT text; // used for detection
    <左、>
     
Right tag control switch for ($ I = 0; $ I <strlen ($ str); $ I ++) {// traverse each character in the processed string if ($ start = 0) & ($ str [$ I] = "> ")) {// if the right tag is detected, use $ start = 1; enable the capture function $ start = 1;} else if ($ start = 1) {// truncation function if ($ str [$ I] = "<") {// if the character is
     <左标签,则使用|替换 $start="0;" $alltext.="|" ; } else if (ord($str[$i])>
      
31) {// if the character is a valid character with ASCII greater than 31, add the character to the $ alltext variable $ alltext. = $ str [$ I] ;}}// the following operations remove spaces and special characters $ alltext = str_replace ("", "", $ alltext ); $ alltext = preg_replace ("/& ([^; &] *) (; | &)/", "", $ alltext ); $ alltext = preg_replace ("/[] +/s", "", $ alltext); return $ alltext ;}
     
    
   
  

The preceding method can also be used to convert the simplified HTML code to TXT text.


Link to this article:

Add to favorites ^ please keep the tutorial address.

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.