這篇文章將為大家詳細(xì)講解有關(guān)怎么在php中批量替換html標(biāo)簽,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
html的全稱為超文本標(biāo)記語言,它是一種標(biāo)記語言,包含了一系列標(biāo)簽.通過這些標(biāo)簽可以將網(wǎng)絡(luò)上的文檔格式統(tǒng)一,使分散的Internet資源連接為一個邏輯整體,html文本是由html命令組成的描述性文本,html命令可以說明文字,圖形、動畫、聲音、表格、鏈接等,主要和css+js配合使用并構(gòu)建優(yōu)雅的前端網(wǎng)頁。
1.把html元素全部去掉,或者保留某幾個html標(biāo)簽
復(fù)制代碼 代碼如下:
$text = '
Test paragraph.
Other text';// Allow
and Test paragraph.
echo strip_tags($text, 'Test paragraph. Other text
2.相反,只去掉某一個html標(biāo)簽
復(fù)制代碼 代碼如下:
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 = 'red text';
$tags = 'font';
$a = strip_only($str, $tags); // red text
$b = strip_only($str, $tags, true); // text
?>
關(guān)于怎么在php中批量替換html標(biāo)簽就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。