>  기사  >  php教程  >  php如何清除html标签(除空格和换行)

php如何清除html标签(除空格和换行)

WBOY
WBOY앞으로
2016-06-07 11:45:281400검색

这篇文章主要讲了php如何清除html标签(除空格和换行),有一定的的参考价值,感兴趣的朋友可以看看,希望对你有所帮助!

方法一,strip_tags()剥去字符串中的 HTML 标签:

<?phpecho strip_tags("Hello <b>world!</b>");//运行结果:Hello world!?>

方法二,自定义函数去除

function cutstr_html($string, $sublen){
    $string = strip_tags($string);    
    $string = preg_replace (&#39;/\n/is&#39;, &#39;&#39;, $string);    
    $string = preg_replace (&#39;/ | /is&#39;, &#39;&#39;, $string);   
    $string = preg_replace (&#39;/ /is&#39;, &#39;&#39;, $string);

    preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $t_string);    
    if(count($t_string[0]) - 0 > $sublen) 
       $string = join(&#39;&#39;, array_slice($t_string[0], 0, $sublen))."…";    
    else 
       $string = join(&#39;&#39;, array_slice($t_string[0], 0, $sublen));   
    return $string;
}

【相关教程推荐

1. php编程从入门到精通全套视频教程 

2. php从入门到精通  

3. bootstrap教程 

성명:
이 기사는 csdn.net에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제