wp_encode_emoji

Convert any 4 byte emoji in a string to their equivalent HTML entity.

Description

(string) wp_encode_emoji( (string) $content ); 

Currently, only Unicode 7 emoji are supported. Skin tone modifiers are allowed, all other Unicode 8 emoji will be added when the spec is finalised.

This allows us to store emoji in a DB using the utf8 character set.

Returns (string)

The encoded content.

Parameters (1)

0. $content (string)
The content to encode.

Usage

  1. if ( !function_exists( 'wp_encode_emoji' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // The content to encode. 
  5. $content = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_encode_emoji($content); 
  9.  

Defined (1)

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

/wp-includes/formatting.php  
  1. function wp_encode_emoji( $content ) { 
  2. if ( function_exists( 'mb_convert_encoding' ) ) { 
  3. $regex = '/( 
  4. \x23\xE2\x83\xA3 # Digits 
  5. [\x30-\x39]\xE2\x83\xA3 
  6. | \xF0\x9F[\x85-\x88][\xA6-\xBF] # Enclosed characters 
  7. | \xF0\x9F[\x8C-\x97][\x80-\xBF] # Misc 
  8. | \xF0\x9F\x98[\x80-\xBF] # Smilies 
  9. | \xF0\x9F\x99[\x80-\x8F] 
  10. | \xF0\x9F\x9A[\x80-\xBF] # Transport and map symbols 
  11. )/x'; 
  12.  
  13. $matches = array(); 
  14. if ( preg_match_all( $regex, $content, $matches ) ) { 
  15. if ( ! empty( $matches[1] ) ) { 
  16. foreach ( $matches[1] as $emoji ) { 
  17. /** 
  18. * UTF-32's hex encoding is the same as HTML's hex encoding. 
  19. * So, by converting the emoji from UTF-8 to UTF-32, we magically 
  20. * get the correct hex encoding. 
  21. */ 
  22. $unpacked = unpack( 'H*', mb_convert_encoding( $emoji, 'UTF-32', 'UTF-8' ) ); 
  23. if ( isset( $unpacked[1] ) ) { 
  24. $entity = '&#x' . ltrim( $unpacked[1], '0' ) . ';'; 
  25. $content = str_replace( $emoji, $entity, $content ); 
  26.  
  27. return $content;