strip_tags

(PHP 4, PHP 5, PHP 7, PHP 8)

strip_tags從字符串中去除 HTML 和 PHP 標(biāo)記

說(shuō)明

strip_tags(string $str, string $allowable_tags = ?): string

該函數(shù)嘗試返回給定的字符串 str 去除空字符、HTML 和 PHP 標(biāo)記后的結(jié)果。它使用與函數(shù) fgetss() 一樣的機(jī)制去除標(biāo)記。

參數(shù)

str

輸入字符串。

allowable_tags

使用可選的第二個(gè)參數(shù)指定不被去除的字符列表。

注意:

HTML 注釋和 PHP 標(biāo)簽也會(huì)被去除。這里是硬編碼處理的,所以無(wú)法通過(guò) allowable_tags 參數(shù)進(jìn)行改變。

注意:

In PHP 5.3.4 and later, self-closing XHTML tags are ignored and only non-self-closing tags should be used in allowable_tags. For example, to allow both <br> and <br/>, you should use:

<?php
strip_tags
($input'<br>');
?>

返回值

返回處理后的字符串。

更新日志

版本 說(shuō)明
5.3.4 strip_tags() ignores self-closing XHTML tags in allowable_tags.
5.0.0 strip_tags() 變?yōu)槎M(jìn)制安全的。

范例

示例 #1 strip_tags() 范例

<?php
$text 
'<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo 
strip_tags($text);
echo 
"\n";

// 允許 <p> 和 <a>
echo strip_tags($text'<p><a>');
?>

以上例程會(huì)輸出:

Test paragraph. Other text
<p>Test paragraph.</p> <a href="#fragment">Other text</a>

注釋

警告

由于 strip_tags() 無(wú)法實(shí)際驗(yàn)證 HTML,不完整或者破損標(biāo)簽將導(dǎo)致更多的數(shù)據(jù)被刪除。

警告

該函數(shù)不會(huì)修改 allowable_tags 參數(shù)中指定的允許標(biāo)記的任何屬性,包括 styleonmouseover 屬性,用戶可能會(huì)在提交的內(nèi)容中惡意濫用這些屬性,從而展示給其他用戶。

注意:

輸入 HTML 標(biāo)簽名字如果大于 1023 字節(jié)(bytes)將會(huì)被認(rèn)為是無(wú)效的,無(wú)論 allowable_tags 參數(shù)是怎樣的。

參見