Heim  >  Artikel  >  php教程  >  PHP清除指定html标签实例代码

PHP清除指定html标签实例代码

WBOY
WBOYOriginal
2016-06-08 17:22:101024Durchsuche

在php中系统为我们内置了一个html标签清除函数strip_tags它删除的是所有字符串了,如果我们只要删除指定的就没有办法了,下面我就来给大家整理了一个站长写的自定义删除html标签函数。

<script>ec(2);</script>


例子

 代码如下 复制代码

   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.'>/iU', $text, $found)){
               $text = str_replace($found[0],$found[1],$text);
         }
       }       return $text;
   }
?>

这个函数很短,但它实现的功能很实用,第一个参数是原字符串,第二个参数是要删除的HTML的标签数组,如果要删除

标签,只需要使用下面的代码:

 代码如下 复制代码

  $tags = array();
$tags[0]='a';
$tags[1]='p';
 $str = "link

help

";
echo strip_selected_tags($str,$tags);
?>

是不是很简单呢?

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn