php 批量替換html標(biāo)簽的實(shí)例代碼
1.把html元素全部去掉,或者保留某幾個(gè)html標(biāo)簽
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "/n";
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>
結(jié)果為(去掉了注釋):
<blockquote>Test paragraph. Other text
<p>Test paragraph.</p> <a href="#fragment">Other text</a></blockquote>2.相反,只去掉某一個(gè)html標(biāo)簽
<?php
function strip_only($str, $tags, $stripContent = false) {
$content = '';
if(!is_array($tags)) {
$tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
if(end($tags) == '') array_pop($tags);
}
foreach($tags as $tag) {
if ($stripContent)
$content = '(.+</'.$tag.'[^>]*>|)';
$str = preg_replace('#</?'.$tag.'[^>]*>'.$content.'#is', '', $str);
}
return $str;
}
$str = '<font color="red">red</font> text';
$tags = 'font';
$a = strip_only($str, $tags); // red text
$b = strip_only($str, $tags, true); // text
?>
- PHP刪除HTMl標(biāo)簽的三種解決方法
- php過濾HTML標(biāo)簽、屬性等正則表達(dá)式匯總
- php去除HTML標(biāo)簽實(shí)例
- PHP html標(biāo)簽正則替換并可自定義正則規(guī)則
- php獲取網(wǎng)頁標(biāo)題和內(nèi)容函數(shù)(不包含html標(biāo)簽)
- PHP實(shí)現(xiàn)HTML標(biāo)簽自動補(bǔ)全代碼
- php使HTML標(biāo)簽自動補(bǔ)全閉合函數(shù)代碼
- PHP中HTML標(biāo)簽過濾技巧
- PHP 修復(fù)未正常關(guān)閉的HTML標(biāo)簽實(shí)現(xiàn)代碼(支持嵌套和就近閉合)
- php實(shí)現(xiàn)過濾表單提交中html標(biāo)簽的方法
- PHP轉(zhuǎn)換文本框內(nèi)容為HTML格式的方法
- PHP將HTML轉(zhuǎn)換成文本的實(shí)現(xiàn)代碼
- php中將html中的br換行符轉(zhuǎn)換為文本輸入中的換行符
- php自定義函數(shù)轉(zhuǎn)換html標(biāo)簽示例
相關(guān)文章
Windows中安裝Apache2和PHP4權(quán)威指南
Windows中安裝Apache2和PHP4權(quán)威指南...2006-11-11

php中使用in_array() foreach array_search() 查找數(shù)組是否包含時(shí)的性能對比