欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP strip_tags保留多個(gè)HTML標(biāo)簽的方法

 更新時(shí)間:2016年05月22日 11:53:23   投稿:mdxy-dxy  
這篇文章主要介紹了PHP strip_tags保留多個(gè)HTML標(biāo)簽的方法,需要的朋友可以參考下

本文介紹了PHP strip_tags函數(shù)保留多個(gè)HTML標(biāo)簽的方法,可以使用第二個(gè)參數(shù)來(lái)設(shè)置不需要?jiǎng)h除的標(biāo)簽,主要涉及到strip_tags的第二個(gè)參數(shù)

strip_tags 函數(shù)

語(yǔ)法
string strip_tags ( string str [, string allowable_tags] )
返回一個(gè)去除了HTML標(biāo)簽的字符串;可以使用第二個(gè)參數(shù)來(lái)設(shè)置不需要?jiǎng)h除的標(biāo)簽。

使用方法:

前提:假如現(xiàn)在有這樣一個(gè)字符串,

復(fù)制代碼 代碼如下:

$str = "<p>我來(lái)自<b><a href='http://www.dbjr.com.cn'>腳本之家</a></b></p>";

1,不保留任何HTML標(biāo)簽,代碼會(huì)是這樣:

復(fù)制代碼 代碼如下:

echo strip_tags($str);
// 輸出:我來(lái)自腳本之家

2,只保留<a>一個(gè)標(biāo)簽的話(huà),只需要將<a>字符串寫(xiě)到strip_tags的第二個(gè)參數(shù)中:
 

復(fù)制代碼 代碼如下:

echo strip_tags($str, "<a>");
// 輸出:我來(lái)自<a href='http://www.dbjr.com.cn'>腳本之家</a>

3,要保留<p>與<b>…多個(gè)標(biāo)簽,只需要將多個(gè)標(biāo)簽用空格分隔后寫(xiě)到strip_tags的第二個(gè)參數(shù)中:
 

復(fù)制代碼 代碼如下:

echo strip_tags($str, "<p> <b>");
// 輸出:<p>我來(lái)自<b>腳本之家</b></p>

如果要使用php刪除html標(biāo)記中的特定標(biāo)簽?zāi)兀?/strong>

這個(gè)就需要代碼來(lái)實(shí)現(xiàn)了,如下:

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.
        '[^>]*>([^<]*)</'.$tag.
        '>/iu', $text, $found)) {
      $text = str_replace($found[0], $found[1], $text);
    }
  }

  return preg_replace('/(<('.join('|', $tags).
    ')( | |.)*/>)/iu', '', $text);
}

$str = "[url="] 123[/url]";
    echo strip_selected_tags($str, array('b'));

相關(guān)文章

最新評(píng)論