删除html标签得到纯文本可处理嵌套的标签
方法基本上来自THinkPHP中的源码,但是被我修改了一下
<div class="codetitle"><a style="CURSOR: pointer" data="17277" class="copybut" id="copybut17277" onclick="doCopy('code17277')"> 代码如下:
<div class="codebody" id="code17277">
<?
PHP /
@Description:
删除HTML
标签,得到纯文本。可以处理嵌套的
标签
/
class deleteHtmlTags{ private $filename; function __construct($filename='C:/AppServ/www/text.txt'){
$this->filename = $filename;
} /*
删除html
标签,得到纯文本。可以处理嵌套的
标签,局限性在于连
标签内的
属性值都会
删除掉
@access public
@p
aram 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,$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;
?>