在php中,可以利用strip_tags()函數(shù)來刪除span標(biāo)簽,該函數(shù)可以剝?nèi)プ址械腍TML、XML以及PHP的標(biāo)簽,語法“strip_tags(string,allow)”;allow是一個(gè)可選參數(shù),用于指定可以被保留下的標(biāo)簽。
本教程操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦
php刪除span標(biāo)簽
在php中,可以利用strip_tags()函數(shù)來刪除span標(biāo)簽。
<?php header("Content-type:text/html;charset=utf-8"); $str = "<span style='color:red;'>Hello</span> world!"; echo $str."<br>"; echo strip_tags($str)."<br>"; ?>
輸出結(jié)果:
strip_tags() 函數(shù)剝?nèi)プ址械?HTML、XML 以及 PHP 的標(biāo)簽。語法格式:
strip_tags(string,allow)
-
string 必需。規(guī)定要檢查的字符串。
-
allow 可選。規(guī)定允許的標(biāo)簽。這些標(biāo)簽不會(huì)被刪除。
<?php header("Content-type:text/html;charset=utf-8"); $str = "<span style='color:red;'>Hello</span> <b>world!</b>"; echo $str."<br>"; echo strip_tags($str)."<br>"; echo strip_tags($str,"<b>")."<br>"; ?>
輸出結(jié)果:
注釋:該函數(shù)始終會(huì)剝離 HTML 注釋。這點(diǎn)無法通過 allow 參數(shù)改變。
推薦學(xué)習(xí):《PHP視頻教程》