strip_tags  好象把所有的标签都截了吧?可能只留下img的嘛?

解决方案 »

  1.   

    回楼上三位,我是想知道这个正则怎样写.
    在php.net上找到这样的方法
    function html2txt($document){
    $search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
                   '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
                   '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
                   '@<![\s\S]*?--[ \t\n\r]*>@'        // Strip multi-line comments including CDATA
    );
    $text = preg_replace($search, '', $document);
    return $text;
    }这个函数可以将所有标签清除,但会连img也清除掉了,如何可以只保留<img />标签,而去掉其他HTML标签,谢谢!
      

  2.   

    这个函数可以将所有标签清除,但会连img也清除掉了,如何可以只保留<img />标签,而去掉其他HTML标签,谢谢!----------------------------
    Replace "<img/>" to "[img/]" before you use the re, and after use the re, replace it back.
      

  3.   

    strip_tags($text,'<img>');
    楼主还顶啊。。陈水给出的这个代码有什么不行吗??
      

  4.   

    晕,大家没看到我这句话吗?
    "但不想用PHP的strip_tags,用preg_replace的话,应该怎样写呢?"
    就是不想用strip_tags啊.谢谢zeroleonhart,陈水,这两个方法我也有想过...但现我还是想知道这个正则表达式怎样写.
      

  5.   

    $str=preg_replace("#<(?!/?img).+?>#","",$str);
      

  6.   

    $str=preg_replace("#<(?!/?img).+?>#","",$str);------------------------------
    If $str = 'we all know 1<2 and 3>2' .........
      

  7.   

    I think you should use preg_replace_callback() to do not replace the img tag.