PHP实现HTML批量转TXT文件

来源:互联网 发布:数据定义语言包括什么 编辑:程序博客网 时间:2024/05/18 20:47
<?phpheader('Content-type:text/html;charset=gb2312');$re = file_get_contents('http://c.biancheng.net/cpp/html/1472.html');html2text($re);function html2text($str){        $str = preg_replace("/<style .*?<\/style>/is", "", $str);    $str = preg_replace("/<script .*?<\/script>/is", "", $str);    $str = preg_replace("/\n|\r/", "", $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("/\&nbsp\;/i", " ", $str);    $str = preg_replace("/\&amp\;/i", "&", $str);    $str = preg_replace("/\&lt\;/i", "<", $str);    $str = preg_replace("/\&gt\;/i", ">", $str);    $str = preg_replace("/\&quot\;/i", '"', $str);    $str = preg_replace("/\&ldquo\;/i", '“', $str);    $str = preg_replace("/\&rdquo\;/i", '”', $str);    $str = preg_replace("/\&lsquo\;/i", "‘", $str);    $str = preg_replace("/\&rsquo\;/i", "’", $str);    $str = preg_replace("/\&mdash\;/i", '—', $str);    $str = preg_replace("/\&hellip\;/i", '…', $str);    $str = preg_replace("/\&middot\;/i", '·', $str);    $str = preg_replace("/\&times\;/i", '×', $str);    //如果有特殊需求,请在本行下面按照以上格式继续加HTML特殊符号和转换后的符号    $str = strip_tags($str);//去除空字符、HTML 和 PHP 标记    $str = html_entity_decode($str, ENT_QUOTES);//解码双引号和单引号 &#039;    $str = preg_replace("/\&\#.*?\;/i", "", $str); //替换所有&#开始;结尾的特殊字符    return $str;}
0 0
原创粉丝点击