wp_specialchars

Legacy escaping for HTML blocks.

Description

(string) _wp_specialchars( (string) $string, (constant) $quote_style = ENT_NOQUOTES, (constant) $charset = false, (bool) $double_encode = false ); 

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.

Returns (string)

The encoded text with HTML entities.

Parameters (4)

0. $string (string)
The text which is to be encoded.
1. $quote_style — Optional. (constant) => ENT_NOQUOTES
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 values; converting single quotes if set to single,, double if set to double or both if otherwise set. Default is ENT_NOQUOTES.
2. $charset — Optional. (constant) => false
The character encoding of the string. Default is false.
3. $double_encode — Optional. (bool) => false
Whether or not to encode existing html entities. Default is false.

Usage

  1. if ( !function_exists( '_wp_specialchars' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-includes/backpress/functions.formatting.php'; 
  3.  
  4. // The text which is to be encoded. 
  5. $string = ''; 
  6.  
  7. // Optional. 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 values; converting single quotes if set to 'single', double if set to 'double' or both if otherwise set. Default is ENT_NOQUOTES. 
  8. $quote_style = ENT_NOQUOTES; 
  9.  
  10. // Optional. The character encoding of the string. Default is false. 
  11. $charset = false; 
  12.  
  13. // Optional. Whether or not to encode existing html entities. Default is false. 
  14. $double_encode = false; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = _wp_specialchars($string, $quote_style, $charset, $double_encode); 
  18.  

Defined (2)

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

/bp-forums/bbpress/bb-includes/backpress/functions.formatting.php  
  1. function _wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) { 
  2. $string = (string) $string; 
  3.  
  4. if ( 0 === strlen( $string ) ) { 
  5. return ''; 
  6.  
  7. // Don't bother if there are no specialchars - saves some processing 
  8. if ( !preg_match( '/[&<>"\']/', $string ) ) { 
  9. return $string; 
  10.  
  11. // Account for the previous behaviour of the function 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. // Store the site charset as a static to avoid multiple calls to backpress_get_option() 
  18. if ( !$charset ) { 
  19. static $_charset; 
  20. if ( !isset( $_charset ) ) { 
  21. $_charset = backpress_get_option( 'charset' ); 
  22. $charset = $_charset; 
  23. if ( in_array( $charset, array( 'utf8', 'utf-8', 'UTF8' ) ) ) { 
  24. $charset = 'UTF-8'; 
  25.  
  26. $_quote_style = $quote_style; 
  27.  
  28. if ( $quote_style === 'double' ) { 
  29. $quote_style = ENT_COMPAT; 
  30. $_quote_style = ENT_COMPAT; 
  31. } elseif ( $quote_style === 'single' ) { 
  32. $quote_style = ENT_NOQUOTES; 
  33.  
  34. // Handle double encoding ourselves 
  35. if ( !$double_encode ) { 
  36. $string = wp_specialchars_decode( $string, $_quote_style ); 
  37. $string = preg_replace( '/&(#?x?[0-9a-z]+);/i', '|wp_entity|$1|/wp_entity|', $string ); 
  38.  
  39. $string = @htmlspecialchars( $string, $quote_style, $charset ); 
  40.  
  41. // Handle double encoding ourselves 
  42. if ( !$double_encode ) { 
  43. $string = str_replace( array( '|wp_entity|', '|/wp_entity|' ), array( '&', ';' ), $string ); 
  44.  
  45. // Backwards compatibility 
  46. if ( 'single' === $_quote_style ) { 
  47. $string = str_replace( "'", ''', $string ); 
  48.  
  49. return $string; 
  1. function wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) { 
  2. if ( func_num_args() > 1 ) { // Maintain backwards compat for people passing additional args 
  3. $args = func_get_args(); 
  4. return call_user_func_array( '_wp_specialchars', $args ); 
  5. } else { 
  6. return esc_html( $string );