搜索
首页php教程php手册PHP将HTML转换成文本一些方法总结

在php中html转换成文本提供了自带的函数strip_tags了,但有时此函数不够用,下面总结了一些用户自定的函数,各位可参考。

最常用的使用php函数strip_tags

<?php
$mystr=<<<SATO
此处省略几十行HTML代码^_^
SATO;
$str=strip_tags($mystr);
//到这里就已经达到我的HTML转为TXT文本的目的了,哈哈,使用这个函数真方便
//下面是插件的一些切词等操作,这里就不多说了
?>

自定义函数

<?php
// $document 应包含一个 HTML 文档。
// 本例将去掉 HTML 标记,javascript 代码
// 和空白字符。还会将一些通用的
// HTML 实体转换成相应的文本。
$search = array ("&#39;<script[^>]*?>.*?</script>&#39;si",  // 去掉 javascript
                 "&#39;<[/!]*?[^<>]*?>&#39;si",           // 去掉 HTML 标记
                 "&#39;([rn])[s]+&#39;",                 // 去掉空白字符
                 "&#39;&(quot|#34);&#39;i",                 // 替换 HTML 实体
                 "&#39;&(amp|#38);&#39;i",
                 "&#39;&(lt|#60);&#39;i",
                 "&#39;&(gt|#62);&#39;i",
                 "&#39;&(nbsp|#160);&#39;i",
                 "&#39;&(iexcl|#161);&#39;i",
                 "&#39;&(cent|#162);&#39;i",
                 "&#39;&(pound|#163);&#39;i",
                 "&#39;&(copy|#169);&#39;i",
                 "&#39;&#(d+);&#39;e");                    // 作为 PHP 代码运行
$replace = array ("",
                  "",
                  "1",
                  "\"",
                  "&",
                  "<",
                  ">",
                  " ",
                  chr(161),
                  chr(162),
                  chr(163),
                  chr(169),
                  "chr(1)");
$text = preg_replace ($search, $replace, $document);
?>

后来我从网上看到了一个使用PHP写的方法,使用这个方法也可以实现将HTML转为TXT文本,个人觉得也还蛮实用的,在这里分享一下,代码如下:

function HtmlToText($str){
  $str=preg_replace("/<sty(.*)/style>|<scr(.*)/script>|<!--(.*)-->/isU","",$str);//去除CSS样式、JS脚本、HTML注释
  $alltext="";//用于保存TXT文本的变量
  $start=1;//用于检测<左、>右标签的控制开关
  for($i=0;$i<strlen($str);$i++){//遍历经过处理后的字符串中的每一个字符
    if(($start==0)&&($str[$i]==">")){//如果检测到>右标签,则使用$start=1;开启截取功能
      $start=1;
    }else if($start==1){//截取功能
      if($str[$i]=="<"){//如果字符是<左标签,则使用<font color=&#39;red&#39;>|</font>替换
        $start=0;
        $alltext.="<font color=&#39;red&#39;>|</font>";
      }else if(ord($str[$i])>31){//如果字符是ASCII大于31的有效字符,则将字符添加到$alltext变量中
        $alltext.=$str[$i];
      }
    }
}
//下方是去除空格和一些特殊字符的操作
$alltext = str_replace(" "," ",$alltext);
$alltext = preg_replace("/&([^;&]*)(;|&)/","",$alltext);
$alltext = preg_replace("/[ ]+/s"," ",$alltext);
return $alltext;
}

使用上面这个方法也可以实现将简答的HTML代码转换为TXT文本。

例3

function html2text($str,$encode = &#39;GB2312&#39;)
{
  $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", "nn", $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("/&nbsp/i", " ", $str);
  $str = preg_replace("/&/i", "&", $str);
  $str = preg_replace("/&amp/i", "&", $str);
  $str = preg_replace("/</i", "<", $str);
  $str = preg_replace("/&lt/i", "<", $str);
  $str = preg_replace("/&ldquo;/i", &#39;"&#39;, $str);
  $str = preg_replace("/&ldquo/i", &#39;"&#39;, $str);
     $str = preg_replace("/&lsquo;/i", "&#39;", $str);
     $str = preg_replace("/&lsquo/i", "&#39;", $str);
     $str = preg_replace("/&rsquo;/i", "&#39;", $str);
     $str = preg_replace("/&rsquo/i", "&#39;", $str);
  $str = preg_replace("/>/i", ">", $str);
  $str = preg_replace("/&gt/i", ">", $str);
  $str = preg_replace("/&rdquo;/i", &#39;"&#39;, $str);
  $str = preg_replace("/&rdquo/i", &#39;"&#39;, $str);
  $str = strip_tags($str);
  $str = html_entity_decode($str, ENT_QUOTES, $encode);
  $str = preg_replace("/&#.*?;/i", "", $str);
     
  return $str;
}


本文地址:

转载随意,但请附上文章地址:-)

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具