php转换html为txt文本的函数代码
本例使用php的preg_replace函数对html中的标记进行替换,以得到纯文本txt内容。
代码如下:
<?php
/**
* 转换html为txt文本的函数
* Edit www.jbxue.com
* at 2013/5/11
*/
function html2text($str){
$str = preg_replace("/<style .*?<\/style>/is", "", $str); $str = preg_replace("/<script .*?<\/script>/is", "", $str);
$str = preg_replace("/<br \s*\/?\/>/i", "\n", $str);
$str = preg_replace("/<\/?p>/i", "\n\n", $str);
$str = preg_replace("/<\/?td>/i", "\n", $str);
$str = preg_replace("/<\/?div>/i", "\n", $str);
$str = preg_replace("/<\/?blockquote>/i", "\n", $str);
$str = preg_replace("/<\/?li>/i", "\n", $str);
$str = preg_replace("/\ \;/i", " ", $str);
$str = preg_replace("/\ /i", " ", $str);
$str = preg_replace("/\&\;/i", "&", $str);
$str = preg_replace("/\&/i", "&", $str);
$str = preg_replace("/\<\;/i", "<", $str);
$str = preg_replace("/\</i", "<", $str);
$str = preg_replace("/\&ldquo\;/i", '"', $str);
$str = preg_replace("/\&ldquo/i", '"', $str);
$str = preg_replace("/\&lsquo\;/i", "'", $str);
$str = preg_replace("/\&lsquo/i", "'", $str);
$str = preg_replace("/\&rsquo\;/i", "'", $str);
$str = preg_replace("/\&rsquo/i", "'", $str);
$str = preg_replace("/\>\;/i", ">", $str);
$str = preg_replace("/\>/i", ">", $str);
$str = preg_replace("/\&rdquo\;/i", '"', $str);
$str = preg_replace("/\&rdquo/i", '"', $str);
$str = strip_tags($str);
$str = html_entity_decode($str, ENT_QUOTES, $encode);
$str = preg_replace("/\&\#.*?\;/i", "", $str);
return $str;
}
?>
您可能感兴趣的文章:
php把html批量转换成txt文件
文本转HTML的php代码
php转换html为txt文本的函数代码
php 文本转HTML与获取IP函数
php不使用iconv库进行gb2312与utf-8编码转换的函数
php读取txt文件内容并赋值给数组
PHP中常用的加密解密方法总结
PHP替换文章内链的函数 php关键词替换(含屏蔽还原原始链接功能)
PHP将HTML转换成纯文本内容实例
设置php页面编码的二种方法