删除html标签得到纯文本可处理嵌套的标签
程序员文章站
2023-11-17 16:27:46
方法基本上来自thinkphp中的源码,但是被我修改了一下 复制代码 代码如下:
方法基本上来自thinkphp中的源码,但是被我修改了一下
<?php
/*
*@description:删除html标签,得到纯文本。可以处理嵌套的标签
*
*/
class deletehtmltags{
private $filename;
function __construct($filename='c:/appserv/www/text.txt'){
$this->filename = $filename;
}
/**
* 删除html标签,得到纯文本。可以处理嵌套的标签,局限性在于连标签内的属性值都会删除掉
* @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;
?>
修改部分也在上面,只是注释掉了。个人觉得这个方法比用正则这类的方法更好。
复制代码 代码如下:
<?php
/*
*@description:删除html标签,得到纯文本。可以处理嵌套的标签
*
*/
class deletehtmltags{
private $filename;
function __construct($filename='c:/appserv/www/text.txt'){
$this->filename = $filename;
}
/**
* 删除html标签,得到纯文本。可以处理嵌套的标签,局限性在于连标签内的属性值都会删除掉
* @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;
?>
修改部分也在上面,只是注释掉了。个人觉得这个方法比用正则这类的方法更好。
上一篇: php环境无法上传文件的解决方法
下一篇: MATLAB如何编写三维球体自旋程序?