wp_specialchars_decode

Converts a number of HTML entities into their special characters.

Description

(string) wp_specialchars_decode( (string) $string, (int) $quote_style = 0 ); 

Specifically deals with: &, <, >, , and ..

$quote_style can be set to ENT_COMPAT to decode entities, or ENT_QUOTES to do both and '. Default is ENT_NOQUOTES where no quotes are decoded.

Returns (string)

The decoded text without HTML entities.

Parameters (2)

0. $string (string)
The text which is to be decoded.
1. $quote_style — Optional. (int)
Converts double quotes if set to ENT_COMPAT, both single and double if set to ENT_QUOTES or none if set to ENT_NOQUOTES. Also compatible with old _wp_specialchars() values; converting single quotes if set to single,, double if set to double or both if otherwise set. Default is ENT_NOQUOTES.

Usage

  1. if ( !function_exists( 'wp_specialchars_decode' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // The text which is to be decoded. 
  5. $string = ''; 
  6. $quote_style = -1; 
  7.  
  8. // NOTICE! Understand what this does before running. 
  9. $result = wp_specialchars_decode($string, $quote_style); 
  10.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/formatting.php  
  1. function wp_specialchars_decode( $string, $quote_style = ENT_NOQUOTES ) { 
  2. $string = (string) $string; 
  3.  
  4. if ( 0 === strlen( $string ) ) { 
  5. return ''; 
  6.  
  7. // Don't bother if there are no entities - saves a lot of processing 
  8. if ( strpos( $string, '&' ) === false ) { 
  9. return $string; 
  10.  
  11. // Match the previous behaviour of _wp_specialchars() when the $quote_style is not an accepted value 
  12. if ( empty( $quote_style ) ) { 
  13. $quote_style = ENT_NOQUOTES; 
  14. } elseif ( !in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) ) { 
  15. $quote_style = ENT_QUOTES; 
  16.  
  17. // More complete than get_html_translation_table( HTML_SPECIALCHARS ) 
  18. $single = array( ''' => '\'', ''' => '\'' ); 
  19. $single_preg = array( '/�*39;/' => ''', '/�*27;/i' => ''' ); 
  20. $double = array( '"' => '"', '"' => '"', '"' => '"' ); 
  21. $double_preg = array( '/�*34;/' => '"', '/�*22;/i' => '"' ); 
  22. $others = array( '<' => '<', '<' => '<', '>' => '>', '>' => '>', '&' => '&', '&' => '&', '&' => '&' ); 
  23. $others_preg = array( '/�*60;/' => '<', '/�*62;/' => '>', '/�*38;/' => '&', '/�*26;/i' => '&' ); 
  24.  
  25. if ( $quote_style === ENT_QUOTES ) { 
  26. $translation = array_merge( $single, $double, $others ); 
  27. $translation_preg = array_merge( $single_preg, $double_preg, $others_preg ); 
  28. } elseif ( $quote_style === ENT_COMPAT || $quote_style === 'double' ) { 
  29. $translation = array_merge( $double, $others ); 
  30. $translation_preg = array_merge( $double_preg, $others_preg ); 
  31. } elseif ( $quote_style === 'single' ) { 
  32. $translation = array_merge( $single, $others ); 
  33. $translation_preg = array_merge( $single_preg, $others_preg ); 
  34. } elseif ( $quote_style === ENT_NOQUOTES ) { 
  35. $translation = $others; 
  36. $translation_preg = $others_preg; 
  37.  
  38. // Remove zero padding on numeric entities 
  39. $string = preg_replace( array_keys( $translation_preg ), array_values( $translation_preg ), $string ); 
  40.  
  41. // Replace characters according to translation table 
  42. return strtr( $string, $translation );