PHP converts HTML into text.

Source: Internet
Author: User
Tags html to 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 commonly used php function strip_tags

The Code is as follows: Copy code


<? Php
$ Mystr = <SATO
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: Copy code

<? Php
// $ 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 [^>] *?>. *? </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: Copy code
Function HtmlToText ($ str ){
$ Str = preg_replace ("/<sty (. *)/style> | <scr (. *)/script> | <! -- (. *) -->/IsU "," ", $ str); // remove CSS styles, JS scripts, HTML comments
$ Alltext = ""; // variable used to save the TXT text
$ Start = 1; // The control switch used to detect the <left,> right label
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.
$ Start = 1;
} Else if ($ start = 1) {// screenshot Function
If ($ str [$ I] = "<") {// if the character is <left tag, replace it with <font color = 'red' >|</font>
$ Start = 0;
$ Alltext. = "<font color = 'red' >|</font> ";
} 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: Copy code

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

$ Str = preg_replace ("/<style .*? </Style>/is "," ", $ str );
$ Str = preg_replace ("/<script .*? </Script>/is "," ", $ str );
$ Str = preg_replace ("/<br s */? />/I "," n ", $ str );
$ Str = preg_replace ("/</? P>/I "," nn ", $ str );
$ Str = preg_replace ("/</? Td>/I "," n ", $ str );
$ Str = preg_replace ("/</? Div>/I "," n ", $ str );
$ Str = preg_replace ("/</? Blockquote>/I "," n ", $ str );
$ Str = preg_replace ("/</? Li>/I "," n ", $ str );

$ Str = preg_replace ("/& nbsp;/I", "", $ str );
$ Str = preg_replace ("/& nbsp/I", "", $ str );

$ Str = preg_replace ("/& amp;/I", "&", $ str );
$ Str = preg_replace ("/& amp/I", "&", $ str );

$ Str = preg_replace ("/& lt;/I", "<", $ str );
$ Str = preg_replace ("/& lt/I", "<", $ str );

$ Str = preg_replace ("/& ldquo;/I", '"', $ str );
$ Str = preg_replace ("/& ldquo/I", '"', $ str );

$ Str = preg_replace ("/& lsquo;/I", "'", $ str );
$ Str = preg_replace ("/& lsquo/I", "'", $ str );

$ Str = preg_replace ("/& rsquo;/I", "'", $ str );
$ Str = preg_replace ("/& rsquo/I", "'", $ str );

$ Str = preg_replace ("/& gt;/I", ">", $ str );
$ Str = preg_replace ("/& gt/I", ">", $ str );

$ Str = preg_replace ("/& rdquo;/I", '"', $ str );
$ Str = preg_replace ("/& rdquo/I", '"', $ str );

$ Str = strip_tags ($ str );
$ Str = html_entity_decode ($ str, ENT_QUOTES, $ encode );
$ Str = preg_replace ("/& amp ("/&#.*?; /I "," ", $ str );

Return $ str;
}

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.