wp_specialchars_decode() │ WP 2.8.0
Converts (decodes) HTML entities to their HTML characters. Changes only: &, <, >, ", '
If you need to encode characters into entities instead, use esc_html()
Keep in mind that this function does not decode all entities. For example, it does not decode – (–).
If you need to decode all characters, use html_entity_decode().
1 time — 0.000094 sec (very fast) | 50000 times — 1.08 sec (fast)
No Hooks.
Returns
String. Decoded text without HTML entities.
Usage
wp_specialchars_decode( $string, $quote_style );
- $string(string) (required)
- The text to be processed.
- $quote_style(string/int)
Determines how to decode quotes. Can be:
ENT_COMPAT or 'double' — change only double quotes: ".
ENT_QUOTES — change both types of quotes: " and '.
'single' — change only single quotes: '.
ENT_NOQUOTES — does not touch quotes at all.
Default: ENT_NOQUOTES
Examples
#1 Replace HTML entities with familiar HTML characters
$string = <<<'TEXT'
ampersand - & / & / &
single quotes - ' / '
double quotation marks - " / " / "
more - > / >
less - < / <
TEXT;
echo wp_specialchars_decode( $string, ENT_QUOTES );
/* will output:
ampersand - & / & / &
single quotes - ' / '
double quotes - " / " / "
more - > / >
less - < / <
*/
Beware that this function does not decode all entities. For example, it does not decode – (–).
If you encounter such an issue, try using html_entity_decode().
Changelog
wp_specialchars_decode() wp specialchars decode code
WP 6.9.1
function wp_specialchars_decode( $text, $quote_style = ENT_NOQUOTES ) {
$text = (string) $text;
if ( 0 === strlen( $text ) ) {
return '';
}
// Don't bother if there are no entities - saves a lot of processing.
if ( ! str_contains( $text, '&' ) ) {
return $text;
}
// Match the previous behavior of _wp_specialchars() 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;
}
// More complete than get_html_translation_table( HTML_SPECIALCHARS ).
$single = array(
''' => '\'',
''' => '\'',
);
$single_preg = array(
'/�*39;/' => ''',
'/�*27;/i' => ''',
);
$double = array(
'"' => '"',
'"' => '"',
'"' => '"',
);
$double_preg = array(
'/�*34;/' => '"',
'/�*22;/i' => '"',
);
$others = array(
'<' => '<',
'<' => '<',
'>' => '>',
'>' => '>',
'&' => '&',
'&' => '&',
'&' => '&',
);
$others_preg = array(
'/�*60;/' => '<',
'/�*62;/' => '>',
'/�*38;/' => '&',
'/�*26;/i' => '&',
);
if ( ENT_QUOTES === $quote_style ) {
$translation = array_merge( $single, $double, $others );
$translation_preg = array_merge( $single_preg, $double_preg, $others_preg );
} elseif ( ENT_COMPAT === $quote_style || 'double' === $quote_style ) {
$translation = array_merge( $double, $others );
$translation_preg = array_merge( $double_preg, $others_preg );
} elseif ( 'single' === $quote_style ) {
$translation = array_merge( $single, $others );
$translation_preg = array_merge( $single_preg, $others_preg );
} elseif ( ENT_NOQUOTES === $quote_style ) {
$translation = $others;
$translation_preg = $others_preg;
}
// Remove zero padding on numeric entities.
$text = preg_replace( array_keys( $translation_preg ), array_values( $translation_preg ), $text );
// Replace characters according to translation table.
return strtr( $text, $translation );
}
Related Functions