|
復(fù)制代碼 代碼如下:
<?php
/*
*@Description:刪除HTML標(biāo)簽,得到純文本。可以處理嵌套的標(biāo)簽
*
*/
class deleteHtmlTags{
private $filename;
function __construct($filename='C:/AppServ/www/text.txt'){
$this->filename = $filename;
}
/**
* 刪除html標(biāo)簽,得到純文本。可以處理嵌套的標(biāo)簽,局限性在于連標(biāo)簽內(nèi)的屬性值都會(huì)刪除掉
* @access public
* @param string $string 要處理的html
* @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;
?>
修改部分也在上面,只是注釋掉了。個(gè)人覺得這個(gè)方法比用正則這類的方法更好。
php技術(shù):刪除html標(biāo)簽得到純文本可處理嵌套的標(biāo)簽,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。