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

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

WBOY
WBOYnach vorne
2016-06-07 11:45:281401Durchsuche

这篇文章主要讲了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教程 

Stellungnahme:
Dieser Artikel ist reproduziert unter:csdn.net. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen
Vorheriger Artikel:jquery控制select只读Nächster Artikel:令牌验证函数单独拿出来