PHP transforms HTML into text some ways to summarize _php tutorials

Source: Internet
Author: User
Tags html comment
In PHP, HTML is converted into text to provide the function strip_tags, but sometimes this function is not enough, the following summarizes some user-defined functions, you can refer to.

The most common use of PHP functions Strip_tags

The code is as follows Copy Code


$mystr =<<<>
Dozens of lines of HTML code are omitted here ^_^
SATO;
$str =strip_tags ($MYSTR);
Here I have reached the purpose of my HTML into txt text, haha, it is very convenient to use this function
Here are some of the plug-in words and other operations, here is not much to say
?>


Custom functions

code as follows copy code
< p> the!--? php
//$document should contain an HTML document.
//This example will remove HTML tags, JavaScript code
//and white space characters. Some generic
//HTML entities are also converted to the appropriate text.

$search = Array ("' Si ',//Remove JavaScript
" ' <[/!] *? [^<>]*?> ' Si ',//Remove HTML Tags
"' ([RN]) [s]+ '",//Remove whitespace characters
"' & (quot| #34); ' I ",//Replace 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

As PHP code $replace = Array ("",
","
"\1", "
" ","
"&",
"<",
">",
"",
Ch R (161),
Chr (162),
Chr (163),
Chr (169),
"Chr (\1)");

$text = preg_replace ($search, $replace, $document);
?

Later I saw from the Internet a way to write using PHP, using this method can also be implemented to convert HTML to txt text, the personal feel is also quite practical, here to share, the code is as follows:

The code is as follows Copy Code
function Htmltotext ($STR) {
$str =preg_replace ("/ | | /isu "," ", $str);//Remove CSS style, JS script, HTML comment
$alltext = "";//variable for saving txt text
$start =1;// <左、> Control switch for detecting the right label
for ($i =0; $i
if ($start ==0) && ($str [$i]== ">") {//if > Right tag is detected, use $start=1; turn on intercept function
$start = 1;
}else if ($start ==1) {//Intercept 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, the character is added to the $alltext variable
$alltext. = $str [$i];
}
}
}
Below are actions to remove spaces and some special characters
$alltext = Str_replace ("", "" ", $alltext);
$alltext = Preg_replace ("/& ([^;&]*) (;|&)/", "", $alltext);
$alltext = Preg_replace ("/[]+/s", "", $alltext);
return $alltext;
}

Using this method can also be implemented to convert the simple HTML code to txt text.

Example 3

The code is as follows Copy Code

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.