源代碼
File: wp-includes/formatting.php
function _wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
$string = (string) $string;
if ( 0 === strlen( $string ) )
return '';
// Don't bother if there are no specialchars - saves some processing
if ( ! preg_match( '/[&<>"\']/', $string ) )
return $string;
// Account for the previous behaviour of the function when the $quote_style is not an accepted value
if ( empty( $quote_style ) )
$quote_style = ENT_NOQUOTES;
elseif ( ! in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) )
$quote_style = ENT_QUOTES;
// Store the site charset as a static to avoid multiple calls to wp_load_alloptions()
if ( ! $charset ) {
static $_charset = null;
if ( ! isset( $_charset ) ) {
$alloptions = wp_load_alloptions();
$_charset = isset( $alloptions['blog_charset'] ) ? $alloptions['blog_charset'] : '';
}
$charset = $_charset;
}
if ( in_array( $charset, array( 'utf8', 'utf-8', 'UTF8' ) ) )
$charset = 'UTF-8';
$_quote_style = $quote_style;
if ( $quote_style === 'double' ) {
$quote_style = ENT_COMPAT;
$_quote_style = ENT_COMPAT;
} elseif ( $quote_style === 'single' ) {
$quote_style = ENT_NOQUOTES;
}
if ( ! $double_encode ) {
// Guarantee every &entity; is valid, convert &garbage; into &garbage;
// This is required for PHP < 5.4.0 because ENT_HTML401 flag is unavailable.
$string = wp_kses_normalize_entities( $string );
}
$string = @htmlspecialchars( $string, $quote_style, $charset, $double_encode );
// Back-compat.
if ( 'single' === $_quote_style )
$string = str_replace( "'", ''', $string );
return $string;
}
更新日志
Version | 描述 |
---|---|
1.2.2 | Introduced. |
在WordPress中,_wp_specialchars()
是一個(gè)內(nèi)部函數(shù),用于轉(zhuǎn)義HTML實(shí)體,以避免XSS攻擊和其他安全問(wèn)題。這個(gè)函數(shù)通常在WordPress內(nèi)部使用,不是公開(kāi)文檔化的,因此不應(yīng)該在公開(kāi)的主題或插件代碼中使用。_wp_specialchars()
函數(shù)類(lèi)似于PHP的 htmlspecialchars()
函數(shù),但是它有一些WordPress特定的默認(rèn)設(shè)置。
以下是 _wp_specialchars()
函數(shù)的基本用法:
string _wp_specialchars( string $text, string $quote_style = ENT_NOQUOTES, string $charset = false, bool $double_encode = false )
參數(shù)解釋如下:
$text
:要轉(zhuǎn)義的字符串。$quote_style
:如何處理引號(hào)。默認(rèn)為ENT_NOQUOTES
,表示不轉(zhuǎn)義引號(hào)。其他可能的值包括ENT_COMPAT
(只轉(zhuǎn)義雙引號(hào))、ENT_QUOTES
(轉(zhuǎn)義雙引號(hào)和單引號(hào))。$charset
:字符集。如果為false
,將使用WordPress的默認(rèn)字符集。$double_encode
:是否對(duì)已經(jīng)轉(zhuǎn)義的實(shí)體進(jìn)行再次轉(zhuǎn)義。默認(rèn)為false
。
以下是一個(gè)使用_wp_specialchars()
函數(shù)的示例:
<?php
// 要轉(zhuǎn)義的字符串
$text = 'The "quick" brown fox jumps over the lazy dog.';
// 使用 _wp_specialchars() 轉(zhuǎn)義字符串
$escaped_text = _wp_specialchars( $text, ENT_QUOTES );
// 輸出轉(zhuǎn)義后的字符串
echo $escaped_text;
?>
在這個(gè)例子中,我們轉(zhuǎn)義了一個(gè)包含引號(hào)的字符串,并且指定了 ENT_QUOTES
作為 $quote_style
參數(shù),這樣單引號(hào)和雙引號(hào)都會(huì)被轉(zhuǎn)義。
需要注意的是,由于 _wp_specialchars()
是一個(gè)內(nèi)部函數(shù),其使用并不推薦在公開(kāi)的主題或插件代碼中。如果你需要在WordPress中轉(zhuǎn)義HTML實(shí)體,應(yīng)該使用WordPress提供的公開(kāi)函數(shù),如 esc_html()
、esc_attr()
或 esc_textarea()
等。這些函數(shù)都是專(zhuān)門(mén)設(shè)計(jì)來(lái)在WordPress中安全地轉(zhuǎn)義輸出內(nèi)容的。
例如,如果你想要轉(zhuǎn)義HTML內(nèi)容,你可以使用:
echo esc_html( $text );
如果你需要轉(zhuǎn)義HTML屬性,你可以使用:
echo esc_attr( $text );
使用這些公開(kāi)函數(shù)可以確保你的代碼與WordPress的未來(lái)版本兼容,并且更加安全。
未經(jīng)允許不得轉(zhuǎn)載:445IT之家 » WordPress函數(shù)_wp_specialchars()用法