The method is basically from the source code in THinkphp, But I modified it.
Copy codeThe Code is as follows:
<? Php
/*
* @ Description: delete HTML tags to obtain plain text. Can process nested labels
*
*/
Class deleteHtmlTags {
Private $ filename;
Function _ construct ($ filename = 'C:/AppServ/www/text.txt '){
$ This-> filename = $ filename;
}
/**
* Delete html tags to obtain plain text. Nested tags can be processed. The restriction is that the attribute values in tags are deleted.
* @ Access public
* @ Param string $ string the html to be processed
* @ Return string
*/
Public function deletehtmltags (){
$ Content = $ this-> contentGet ();
While (strstr ($ content, '> ')){
$ CurrentBegin = strpos ($ content, '<');
$ CurrentEnd = strpos ($ content, '> ');
$ Cha = $ currentEnd-$ currentBegin-1;
$ TmpStringBegin = @ substr ($ content, 0, $ currentBegin );
// $ TmpStringMiddle = @ substr ($ content, $ currentBegin + 1, $ cha );
$ TmpStringEnd = @ substr ($ content, $ currentEnd + 1, strlen ($ content ));
// $ Content = $ tmpStringBegin. $ tmpStringMiddle. $ tmpStringEnd;
$ Content = $ tmpStringBegin. $ tmpStringEnd;
}
Return $ content;
}
Private function contentGet (){
$ Fd = fopen ($ this-> filename, 'R ');
$ Content = fread ($ fd, filesize ($ this-> filename ));
Fclose ($ fd );
Return $ content;
}
}
$ DeleteHtml = new deleteHtmlTags ();
$ Content = $ deleteHtml-> deletehtmltags ();
Echo $ content;
?>
The modified part is also above, but commented out. I personally think this method is better than regular expressions.