Php Chinese encoding judgment code
- Preg_replace ("/([\ x80-\ xff])/", "", $ str );
- Preg_replace ("/([u4e00-u9fa5])/", ", $ str );
Example: php Chinese encoding judgment.
- // Determine whether the content contains Chinese characters-gbk (php)
- Function check_is_chinese ($ s ){
- Return preg_match ('/[\ x80-\ xff]./', $ s );
- }
- // Obtain the string length-gbk (php)
- Function gb_strlen ($ str ){
- $ Count = 0;
- For ($ I = 0; $ I $ S = substr ($ str, $ I, 1 );
- If (preg_match ("/[\ x80-\ xff]/", $ s) ++ $ I;
- + + $ Count;
- }
- Return $ count;
- }
- // Truncate the string-gbk (php)
- Function gb_substr ($ str, $ len ){
- $ Count = 0;
- For ($ I = 0; $ I If ($ count = $ len) break;
- If (preg_match ("/[\ x80-\ xff]/", substr ($ str, $ I, 1) ++ $ I;
- + + $ Count;
- }
- Return substr ($ str, 0, $ I );
- }
- // Count the length of the string-utf8 (php)
- Function utf8_strlen ($ str ){
- $ Count = 0;
- For ($ I = 0; $ I <strlen ($ str); $ I ++ ){
- $ Value = ord ($ str [$ I]);
- If ($ value & gt; 127 ){
- $ Count ++;
- If ($ value >=192 & $ value <= 223) $ I ++;
- Elseif ($ value >=224 & $ value <= 239) $ I = $ I + 2;
- Elseif ($ value >=240 & $ value <= 247) $ I = $ I + 3;
- Else die ('not a UTF-8 compatible string ');
- }
- $ Count ++;
- }
- Return $ count;
- }
- // Truncate the string-utf8 (php)
- Function utf8_substr ($ str, $ position, $ length ){
- $ Start_position = strlen ($ str );
- $ Start_byte = 0;
- $ End_position = strlen ($ str );
- $ Count = 0;
- For ($ I = 0; $ I <strlen ($ str); $ I ++ ){
- If ($ count >=$ position & $ start_position> $ I ){
- $ Start_position = $ I;
- $ Start_byte = $ count;
- }
- If ($ count-$ start_byte)> = $ length ){
- $ End_position = $ I;
- Break;
- }
- $ Value = ord ($ str [$ I]);
- If ($ value & gt; 127 ){
- $ Count ++;
- If ($ value >=192 & $ value <= 223) $ I ++;
- Elseif ($ value >=224 & $ value <= 239) $ I = $ I + 2;
- Elseif ($ value >=240 & $ value <= 247) $ I = $ I + 3;
- Else die ('not a UTF-8 compatible string ');
- }
- $ Count ++;
- }
- Return (substr ($ str, $ start_position, $ end_position-$ start_position ));
- }
- // Determine whether there is Korean-UTF-8 (javascript)
- Function checkkoreachar (str ){
- For (I = 0; I If (str. charcodeat (I)> 0x3130 & str. charcodeat (I) <0x318f) | (str. charcodeat (I)> = 0xac00 & str. charcodeat (I) <= 0xd7a3 ))){
- Return true;
- }
- }
- Return false;
- }
- // Determine whether a Chinese character-gbk (javascript) exists)
- Function check_chinese_char (s ){
- Return (s. length! = S. replace (/[^ \ x00-\ xff]/g, "**"). length );
- }
|