当前位置:编程学习 > php >>

php编程去除html的各种方法自带函数正则表达式

用php自带函数去除html标记

 strip_tags

  去掉 HTML 及 PHP 的标记。

  语法: string strip_tags(string str);

  传回值: 字串

  函式种类: 资料处理

  内容说明

  本函式可去掉字串中包含的任何 HTML 及 PHP 的标记字串。若是字串的 HTML 及 PHP 标签原来就有错,例如少了大于的符号,则也会传回错误。而本函式和 fgetss() 有着相同的功能。

  htmlspecialchars

  将特殊字元转成 HTML 格式。

  语法: string htmlspecialchars(string string);

  传回值: 字串

  函式种类: 资料处理

  本函式将特殊字元转成 HTML 的字串格式 ( &....; )。最常用到的场合可能就是处理客户留言的留言版了。

  & (和) 转成 &
  " (双引号) 转成 "
  < (小于) 转成 <
  > (大于) 转成 >
  此函式只转换上面的特殊字元,并不会全部转换成 HTML 所定的 ASCII 转换。

  使用范例
 

<?php 
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES); 
echo $new; 
?> 

------------------------------------------------------------------------------
PHP如何去除HTML标签

PHP 中有个 strip_tags 函数可以方便地去除 HTML 标签。
echo strip_tags(“Hello <b>World</b>”); // 去除 HTML、XML 以及 PHP 的标签。
对于非标准的 HTML 代码也能正确的去除:
echo strip_tags(“<a href=\”>\”>cftea</a>”); //输出 cftea

在PHP中可以使用strip_tags函数去除HTML标签,看下面示例:
<?php
$str = ‘www<p>dreamdu</p>.com’;
echo(htmlspecialchars($str).”<br>”);
echo(strip_tags($str));
?>

----------------------------

PHP如何去除HTML标签


方法1:
直接取出想要取出的标记
<?php
    //取出br标记
    function strip($str)
{
$str=str_replace("<br>","",$str);
//$str=htmlspecialchars($str);
return strip_tags($str);
}
       ?>
方法2.
PHP 中有个 strip_tags 函数可以方便地去除 HTML 标签。
echo strip_tags(“Hello <b>World</b>”); // 去除 HTML、XML 以及 PHP 的标签。
对于非标准的 HTML 代码也能正确的去除:
echo strip_tags(“<a href=\”>\”>cftea</a>”); //输出 cftea

在PHP中可以使用strip_tags函数去除HTML标签,看下面示例:
<?php
$str = ‘www<p>dreamdu</p>.com’;
echo(htmlspecialchars($str).”<br>”);
echo(strip_tags($str));
?>

方法3.
strtr() 函数转换字符串中特定的字符。

语法
strtr(string,from,to)
或者
strtr(string,array)
参数  描述
string1  必需。规定要转换的字符串。
from  必需(除非使用数组)。规定要改变的字符。
to  必需(除非使用数组)。规定要改变为的字符。
array  必需(除非使用 from 和 to)。一个数组,其中的键是原始字符,值是目标字符。例子1:
<?php
echo strtr("Hilla Warld","ia","eo");
?>
例子2:
<?php
$arr = array("Hello" => "Hi", "world" => "earth");
echo strtr("Hello world",$arr);
?>

--------------------------------------------------------------------

php 去掉html标签 

function noHTML($content)
    {
     $content = preg_replace("/<a[^>]*>/i",'', $content);  
  $content = preg_replace("/<\/a>/i", '', $content);   
  $content = preg_replace("/<div[^>]*>/i",'', $content);  
  $content = preg_replace("/<\/div>/i",'', $content);
  $content = preg_replace("/<font[^>]*>/i",'', $content);  
  $content = preg_replace("/<\/font>/i",'', $content);
  $content = preg_replace("/<p[^>]*>/i",'', $content);  
  $content = preg_replace("/<\/p>/i",'', $content);
  $content = preg_replace("/<span[^>]*>/i",'', $content);  
  $content = preg_replace("/<\/span>/i",'', $content);
  $content = preg_replace("/<\?xml[^>]*>/i",'', $content);
  $content = preg_replace("/<\/\?xml>/i",'', $content);
  $content = preg_replace("/<o:p[^>]*>/i",'', $content);
  $content = preg_replace("/<\/o:p>/i",'', $content);
  $content = preg_replace("/<u[^>]*>/i",'', $content);
  $content = preg_replace("/<\/u>/i",'', $content);
  $content = preg_replace("/<b[^>]*>/i",'', $content);
  $content = preg_replace("/<\/b>/i",'', $content);
  $content = preg_replace("/<meta[^>]*>/i",'', $content);
       $content = preg_replace("/<\/meta>/i",'', $content);
  $content = preg_replace("/<!--[^>]*-->/i",'', $content);//注释内容 
  $content = preg_replace("/<p[^>]*-->/i",'', $content);//注释内容      
  $content = preg_replace("/style=.+?['|\"]/i",'',$content);//去除样式  
  $content = preg_replace("/class=.+?['|\"]/i",'',$content);//去除样式  
  $content = preg_replace("/id=.+?['|\"]/i",'',$content);//去除样式     
  $content = preg_replace("/lang=.+?['|\"]/i",'',$content);//去除样式      
  $content = preg_replace("/width=.+?['|\"]/i",'',$content);//去除样式   
  $content = preg_replace("/height=.+?['|\"]/i",'',$content);//去除样式   
  $content = preg_replace("/border=.+?['|\"]/i",'',$content);//去除样式   
  $content = preg_replace("/face=.+?['|\"]/i",'',$content);//去除样式
     $content = preg_replace("/face=.+?['|\"]/",'',$content);
     $content = preg_replace("/face=.+?['|\"]/",'',$content);
     $content=str_replace( " ","",$content);
     return $content;
    }

-------------------------------------------------------------

PHP去掉指定HTML标签 

//去掉 指定的html标签
 public function strip_selected_tags($text, $tags = array())
   {
       $args = func_get_args();
       $text = array_shift($args);
       $tags = func_num_args() > 2 ? array_diff($args,array($text))  : (array)$tags;
       foreach ($tags as $tag){
           if(preg_match_all('/<'.$tag.'[^>]*>(.*)<\/'.$tag.'>/iU', $text, $found)){
               $text = str_replace($found[0],$found[1],$text);
         }
       }

       return $text;
   }

 

调用

 $str= Common_Filter::strip_selected_tags($tmpstr,'p');//去掉p标签,要保证字符串$tmpstr中HTML标签是<p>形式,如果是转义后的,<p> 形式的,则需要用“htmlspecialchars_decode”转义回来,

如:$str= Common_Filter::strip_selected_tags(htmlspecialchars_decode($tmpstr),'p'); 如果想去掉所有HTML标签,还有更方便PHP自带函数,strip_tags(),直接使用会去掉所有标签,第二参数可以是需要保留的标签,如下例:

$text = '<p>Test paragraph.</p><!-- Comment --> Other text';
echo strip_tags($text);
// Allow <p>
echo strip_tags($text, '<p>');
上例将输出:

Test paragraph. Other text
<p>Test paragraph.</p> Other text

-------------------------------------------------------
php正则表达式去除html标签


$str = "<div style='color:red'>test string</div><br />";
$preg = "/<\/?[^>]+>/i";
echo $string;
echo preg_replace($preg,'',$str);

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,