Php Chinese character unicode encoding and decoding
// Encode the content in unicode format: yoka \ u738b (original: yoka King)
- Function unicode_encode ($ name)
- {
- $ Name = iconv ('utf-8', 'ucs-2', $ name );
- $ Len = strlen ($ name );
- $ Str = '';
- For ($ I = 0; $ I <$ len-1; $ I = $ I + 2)
- {
- $ C = $ name [$ I];
- $ C2 = $ name [$ I + 1];
- If (ord ($ c)> 0)
- {// Two-byte text
- $ Str. = '\ U'. base_convert (ord ($ c), 10, 16). base_convert (ord ($ c2), 10, 16 );
- }
- Else
- {
- $ Str. = $ c2;
- }
- }
- Return $ str;
- } // (Script School bbs.it-home.org editing)
// Decodes the unicode encoded content. the encoded content format is yoka \ u738b (original: yoka King)
- Function unicode_decode ($ name)
- {
- // Convert the unicode encoding to the UTF-8 encoding that can be viewed
- $ Pattern = '/([\ w] +) | (\ u ([\ w] {4})/I ';
- Preg_match_all ($ pattern, $ name, $ matches );
- If (! Empty ($ matches ))
- {
- $ Name = '';
- For ($ j = 0; $ j <count ($ matches [0]); $ j ++)
- {
- $ Str = $ matches [0] [$ j];
- If (strpos ($ str, '\ u') = 0)
- {
- $ Code = base_convert (substr ($ str, 2, 2), 16, 10 );
- $ Code2 = base_convert (substr ($ str, 4), 16, 10 );
- $ C = chr ($ code). chr ($ code2 );
- $ C = iconv ('ucs-2', 'utf-8', $ c );
- $ Name. = $ c;
- }
- Else
- {
- $ Name. = $ str;
- }
- }
- }
- Return $ name;
- }
Test:
- Echo 'yoka \ u738b-> '. unicode_decode ('yoka \ u738b ').'';
- $ Name = 'yoka King ';
- Echo ''. unicode_encode ($ name ).'';
Note: The Sina blog editor filters out /***/. |