Home  >  Article  >  Backend Development  >  Delete html tags to get plain text and handle nested tags_PHP tutorial

Delete html tags to get plain text and handle nested tags_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:31:33728browse

The method basically comes from the source code in THinkphp, but I modified it a bit

Copy the code The code is as follows:

/*
*@Description: Remove HTML tags and get plain text. Can handle nested tags
*
*/
class deleteHtmlTags{

private $filename;

function __construct($filename='C:/AppServ/www /text.txt'){
$this->filename = $filename;
}

/**
* Remove html tags and get plain text. Nested tags can be processed, but the limitation is that even the attribute values ​​​​in the tags will be deleted
* @access public
* @param string $string 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, just commented out. Personally, I think this method is better than using regular methods.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/762211.htmlTechArticleThe method basically comes from the source code in THinkphp, but it was modified by me. The copy code is as follows: ?php /* *@Description: Remove HTML tags and get plain text. Can handle nested tags...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn