正则去除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( "&nbsp;","",$content);
    return $content;
    }
本文作者: TMs
本文链接: https://blog.tms.im/2014/08/22/nohtml.html
版权声明: 本作品采用 CC BY-NC-SA 3.0 CN 进行许可。转载请注明出处!
知识共享许可协议