教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP将HTML转换成文本一些方法总结

PHP将HTML转换成文本一些方法总结

发布时间:2016-12-03   编辑:jiaochengji.com
教程集为您提供PHP将HTML转换成文本一些方法总结等资源,欢迎您收藏本站,我们将为您提供最新的PHP将HTML转换成文本一些方法总结资源
在php中html转换成文本提供了自带的函数strip_tags了,但有时此函数不够用,下面总结了一些用户自定的函数,各位可参考。

最常用的使用php函数strip_tags

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4827')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4827>


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


自定义函数

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3493')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3493>

<?php
// $document 应包含一个 HTML 文档。
// 本例将去掉 HTML 标记,javascript 代码
// 和空白字符。还会将一些通用的
// HTML 实体转换成相应的文本。

$search = array ("'<script[^>]*?>.*?</script>'si",  // 去掉 javascript
                 "'<[/!]*?[^<>]*?>'si",           // 去掉 HTML 标记
                 "'([rn])[s] '",                 // 去掉空白字符
                 "'&(quot|#34);'i",                 // 替换 HTML 实体
                 "'&(amp|#38);'i",
                 "'&(lt|#60);'i",
                 "'&(gt|#62);'i",
                 "'&(nbsp|#160);'i",
                 "'&(iexcl|#161);'i",
                 "'&(cent|#162);'i",
                 "'&(pound|#163);'i",
                 "'&(copy|#169);'i",
                 "'&#(d );'e");                    // 作为 PHP 代码运行

$replace = array ("",
                  "",
                  "\1",
                  """,
                  "&",
                  "<",
                  ">",
                  " ",
                  chr(161),
                  chr(162),
                  chr(163),
                  chr(169),
                  "chr(\1)");

$text = preg_replace ($search, $replace, $document);
?>

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

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9374')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9374>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='red'>|</font>替换
        $start=0;
        $alltext.="<font color='red'>|</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

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7974')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7974>

function html2text($str,$encode = 'GB2312')
{

  $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("/&nbsp;/i", " ", $str);
  $str = preg_replace("/&nbsp/i", " ", $str);

  $str = preg_replace("/&amp;/i", "&", $str);
  $str = preg_replace("/&amp/i", "&", $str);

  $str = preg_replace("/&lt;/i", "<", $str);
  $str = preg_replace("/&lt/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("/&gt;/i", ">", $str);
  $str = preg_replace("/&gt/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数组编码转换小例子
php mysql转义特殊字符函数
php数组编码转换的方法参考
PHP汉字拼音转换、公历农历转换的实例详解
PHP如何将十进制转换为十六进制?(代码示例)
php把html批量转换成txt文件
php表单提交特殊字符过滤方法
PHP的安装
php5 字符串处理函数汇总
php数字类型之字符串类型详解

[关闭]
~ ~