_wp_specialchars()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
_wp_specialchars( string $text, int|string $quote_style = ENT_NOQUOTES, false|string $charset = false, bool $double_encode = false ): string
Описание
В частности обрабатывает: &, , " и '.
$quote_style может быть равен ENT_COMPAT, чтобы кодировать " в ", или ENT_QUOTES, чтобы кодировать оба вида кавычек. Значение по умолчанию — ENT_NOQUOTES, при котором кавычки не кодируются.
Оригинал (английский)
Specifically deals with: &, <, >, ", and '.
$quote_style can be set to ENT_COMPAT to encode " to ", or ENT_QUOTES to do both. Default is ENT_NOQUOTES where no quotes are encoded.
Параметры
$text
string
обязательный
$quote_style
int|string
необязательный
= ENT_NOQUOTES
$charset
false|string
необязательный
= false
$double_encode
bool
необязательный
= false
Возвращаемое значение
string
Исходный код
wp-includes/formatting.php:945
function _wp_specialchars( $text, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
$text = (string) $text;
if ( 0 === strlen( $text ) ) {
return '';
}
// Don't bother if there are no specialchars - saves some processing.
if ( ! preg_match( '/[&<>"\']/', $text ) ) {
return $text;
}
// Account for the previous behavior of the function when the $quote_style is not an accepted value.
if ( empty( $quote_style ) ) {
$quote_style = ENT_NOQUOTES;
} elseif ( ENT_XML1 === $quote_style ) {
$quote_style = ENT_QUOTES | ENT_XML1;
} elseif ( ! in_array( $quote_style, array( ENT_NOQUOTES, ENT_COMPAT, ENT_QUOTES, 'single', 'double' ), true ) ) {
$quote_style = ENT_QUOTES;
}
$charset = _canonical_charset( $charset ? $charset : get_option( 'blog_charset' ) );
$_quote_style = $quote_style;
if ( 'double' === $quote_style ) {
$quote_style = ENT_COMPAT;
$_quote_style = ENT_COMPAT;
} elseif ( 'single' === $quote_style ) {
$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.
*/
$text = wp_kses_normalize_entities( $text, ( $quote_style & ENT_XML1 ) ? 'xml' : 'html' );
}
$text = htmlspecialchars( $text, $quote_style, $charset, $double_encode );
// Back-compat.
if ( 'single' === $_quote_style ) {
$text = str_replace( "'", ''', $text );
}
return $text;
}
История изменений
| Версия | Описание |
|---|---|
| 5.5.0 | $quote_style also accepts ENT_XML1. |
| 1.2.2 | Introduced. |

