Some methods to convert HTML into text in PHP _ PHP Tutorial

Source: Internet
Author: User
Tags html to text
PHP converts HTML into text. In php, the built-in strip_tags function is provided for converting html to text, but sometimes this function is not enough. The following summarizes some user-defined functions for your reference. The most common function of converting html to text using p in php provides the built-in function strip_tags, but sometimes this function is not enough. The following summarizes some user-defined functions for your reference.

The most commonly used php function strip_tags

The code is as follows:


$ Mystr = < Dozens of lines of HTML code are omitted here.
SATO;
$ Str = strip_tags ($ mystr );
// 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.
?>


Custom functions

The code is as follows:

// $ Document should contain an HTML document.
// In this example, the HTML tag and javascript code are removed.
// And blank characters. Some common
// Convert the HTML object to the corresponding text.

$ Search = array ("' ] *?>. *? Script 'Si ", // remove javascript
"'<[/!] *? [^ <>] *?> 'Si ", // remove the HTML tag
"'([Rn]) [s] +'", // remove the white space
"'& (Quot | #34);' I", // replaces the HTML object
"'& (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 as PHP code

$ Replace = array ("",
"",
"\ 1 ",
""",
"&",
"<",
"> ",
"",
Chr (1, 161 ),
Chr (1, 162 ),
Chr (1, 163 ),
Chr (1, 169 ),
"Chr (\ 1 )");

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

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:

The code is as follows:
Function HtmlToText ($ str ){
$ Str = preg_replace ("/ | | /IsU "," ", $ str); // remove CSS styles, JS scripts, and HTML comments.
$ Alltext = ""; // variable used to save the TXT text
$ Start = 1; // used for detection <左、> Control switch of right label
For ($ I = 0; $ I If ($ start = 0) & ($ str [$ I] = ">") {// if the> right tag is detected, use $ start = 1.
$ Start = 1;
} Else if ($ start = 1) {// screenshot 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];
}
}
}
// Remove spaces and special characters at the bottom
$ 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 into TXT text.

Example 3

The code is as follows:

Function html2text ($ str, $ encode = 'gb2312 ')
{

$ Str = preg_replace ("/

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.