php中tags,php中strip_tags()函数的用法举例
在php中strip_tags() 函數(shù),可以剝?nèi)?HTML、XML 以及 PHP 的標(biāo)簽。
用法:
strip_tags(string,allow)
后面的allow是可選的。填入的話表示什么標(biāo)簽被允許。
附,php使用strip_tags清除所有標(biāo)記。
string strip_tags(string str);
函數(shù)strip_tags可去掉字符串中包含的任何 HTML 及 PHP 的標(biāo)記字符串。若是字符串的 HTML 及 PHP 標(biāo)簽原來就有錯,例如少了大于的符號,則也會返回錯誤。
string strip_tags ( string str [, string allowable_tags] ) 返回一個去除了HTML標(biāo)簽的字符串;可以使用第二個參數(shù)來設(shè)置不需要刪除的標(biāo)簽。
例如:
strip_tags($str, "");
//保留$str中的a標(biāo)簽
?>
問題1,strip_tags如何保留多個HTML標(biāo)簽?
只需要將多個標(biāo)簽用空格分隔后寫到strip_tags的第二個參數(shù)中,代碼:
strip_tags($str, "
");
問題2,php刪除html標(biāo)記中的特定標(biāo)簽的方法?
代碼:
/**
* 刪除html標(biāo)記中的特定標(biāo)簽
* edit www.jbxue.com
*/
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( '/]*>([^/iu', $text, $found) ){
$text = str_replace($found[0],$found[1],$text);
}
}
return preg_replace( '/()/iu', '', $text);
}
$str = "[url="]123[/url]";
echo strip_selected_tags($str,array('b'));
?>
總結(jié)
以上是生活随笔為你收集整理的php中tags,php中strip_tags()函数的用法举例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 甘肃张掖遭遇沙尘暴:沙墙高达百米
- 下一篇: 长安汽车:2023 年将实现更多新技术量