Php Chinese support function php Chinese intercept string function PHP Chinese character string flip

Source: Internet
Author: User
Php Chinese support function php Chinese intercept string function PHP Chinese character string flip

  1. /**
  2. * Convert a string containing full-width numeric characters, letters, spaces, or '% +-()' to the corresponding half-width characters.
  3. *
  4. * @ Access public
  5. * @ Param string $ str string to be converted
  6. *
  7. * @ Return string $ str string after processing
  8. */
  9. Function make_semiangle ($ str)
  10. {
  11. $ Arr = array ('0' => '0', '1' => '1', '2' => '2 ', '3' => '3', '4' => '4 ',
  12. '5' => '5', '6' => '6', '7' => '7', '8' => '8 ', '9' => '9 ',
  13. 'A' => 'A', 'B' => 'B', 'c' => 'C', 'D' => 'D ', 'E' => 'e ',
  14. 'F' => 'F', 'G' => 'G', 'H' => 'h', 'I' => 'I ', 'j' => 'J ',
  15. 'K' => 'K', 'L' => 'L', 'M' => 'm', 'n' => 'N ', 'o' => 'O ',
  16. 'P' => 'P', 'q' => 'Q', 'R' => 'R', 's' =>'s ', 'T' => 'T ',
  17. 'U' => 'u', 'V' => 'V', 'W' => 'W', 'x' => 'X ', 'y' => 'y ',
  18. 'Z' => 'Z', 'a' => 'A', 'B' => 'B', 'c' => 'C ', 'D' => 'D ',
  19. 'E' => 'e', 'F' => 'F', 'G' => 'G', 'H' => 'h ', 'I' => 'I ',
  20. 'J' => 'J', 'K' => 'K', 'L' => 'L', 'M' =>'m ', 'n' => 'N ',
  21. 'O' => 'O', 'P' => 'P', 'q' => 'Q', 'R' => 'R ','s '=>'s ',
  22. 'T' => 'T', 'u' => 'u', 'V' => 'V', 'W' => 'W ', 'X' => 'X ',
  23. 'Y' => 'y', 'Z' => 'Z ',
  24. '(' => '(', ')' => ')', '[' => '[', ']' => ']', '[' => '[',
  25. ']' => ']', '<' => '[', '<' => ']', '=> '[', '"' => ']',
  26. ''' => '[', ''' => ']', '{' => '{', '}' => '}', '=>' <',
  27. '"' => ',
  28. '%' => '%', '+' => '+', '-' => '-', '-' => '-','~ '=> '-',
  29. ':' => ':','. '=>'. ',', '=>', '=>'. ',', '=> '.',
  30. ';' => ',','? '=> '? ','! '=> '! ','... '=>'-', ''' =>' | ',
  31. '"' => '"', ''' => ''', ''' => ''', '|' => '| ', 'Region' => '"',
  32. ''=>'', '$' => '$', '@' => '@', '#' => '#', '^' => '^', '&' => '&', '*' => '*');
  33. Return strtr ($ str, $ arr );
  34. }

Example 2: php Chinese truncation string

  1. /*
  2. Chinese character truncation functions supported by Utf-8 and gb2312
  3. Cut_str (string, truncation length, start length, encoding );
  4. The default encoding format is UTF-8.
  5. The default start length is 0.
  6. * Edit: bbs.it-home.org
  7. */
  8. Function cut_str ($ string, $ sublen, $ start = 0, $ code = 'utf-8 '){
  9. If ($ code = 'utf-8 '){
  10. $ Pa = "/[\ x01-\ x7f] | [\ xc2-\ xdf] [\ x80-\ xbf] | \ xe0 [\ xa0-\ xbf] [\ x80 -\ xbf] | [\ xe1-\ xef] [\ x80-\ xbf] [\ x80-\ xbf] | \ xf0 [\ x90-\ xbf] [\ x80- \ xbf] [\ x80-\ xbf] | [\ xf1-\ xf7] [\ x80-\ xbf] [\ x80-\ xbf] [\ x80-\ xbf]/ ";
  11. Preg_match_all ($ pa, $ string, $ t_string );
  12. If (count ($ t_string [0])-$ start> $ sublen)
  13. Return join ('', array_slice ($ t_string [0], $ start, $ sublen ))."...";
  14. Return join ('', array_slice ($ t_string [0], $ start, $ sublen ));
  15. } Else {
  16. $ Start = $ start * 2;
  17. $ Sublen = $ sublen * 2;
  18. $ Strlen = strlen ($ string );
  19. $ Tmpstr = '';
  20. For ($ I = 0; $ I <$ strlen; $ I ++ ){
  21. If ($ I >=$ start & $ I <($ start + $ sublen )){
  22. If (ord (substr ($ string, $ I, 1) & gt; 129 ){
  23. $ Tmpstr. = substr ($ string, $ I, 2 );
  24. } Else {
  25. $ Tmpstr. = substr ($ string, $ I, 1 );
  26. }
  27. }
  28. If (ord (substr ($ string, $ I, 1)> 129)
  29. $ I ++;
  30. }
  31. If (strlen ($ tmpstr) <$ strlen)
  32. $ Tmpstr. = "...";
  33. Return $ tmpstr;
  34. }
  35. }
  36. $ Str = "the string to be intercepted by abcd ";
  37. Echo cut_str ($ str, 1, 0, 'gb2312 ');
  38. ?>

Example 3: string loading

  1. /**
  2. * UTF-8/GBK character truncation is supported.
  3. * @ Param $ string
  4. * @ Param $ length
  5. * @ Param $ dot
  6. */
  7. Function str_cut ($ string, $ length, $ charset = 'utf-8', $ dot = '...'){
  8. $ Strlen = strlen ($ string );
  9. If ($ strlen <= $ length) return $ string;
  10. $ String = str_replace (array ('','', '&', '"', ''', '-', '<', '> ','·','... '), Array ('region', '',' & ','" ', "'", ''', ','-',' <', '> ','·','... '), $ String );
  11. $ Strcut = '';
  12. If (strtolower ($ charset) = 'utf-8 '){
  13. $ Length = intval ($ length-strlen ($ dot)-$ length/3 );
  14. $ N = $ tn = $ noc = 0;
  15. While ($ n <strlen ($ string )){
  16. $ T = ord ($ string [$ n]);
  17. If ($ t = 9 | $ t = 10 | (32 <= $ t & $ t <= 126 )){
  18. $ Tn = 1; $ n ++; $ noc ++;
  19. } Elseif (194 <=$ t & $ t <= 223 ){
  20. $ Tn = 2; $ n + = 2; $ noc + = 2;
  21. } Elseif (224 <=$ t & $ t <= 239 ){
  22. $ Tn = 3; $ n + = 3; $ noc + = 2;
  23. } Elseif (240 <=$ t & $ t <= 247 ){
  24. $ Tn = 4; $ n + = 4; $ noc + = 2;
  25. } Elseif (248 <=$ t & $ t <= 251 ){
  26. $ Tn = 5; $ n + = 5; $ noc + = 2;
  27. } Elseif ($ t = 252 | $ t = 253 ){
  28. $ Tn = 6; $ n + = 6; $ noc + = 2;
  29. } Else {
  30. $ N ++;
  31. }
  32. If ($ noc >=$ length ){
  33. Break;
  34. }
  35. }
  36. If ($ noc> $ length ){
  37. $ N-= $ tn;
  38. }
  39. $ Strcut = substr ($ string, 0, $ n );
  40. $ Strcut = str_replace (array ('hour', '&', '"'," '",'-',' <', '> ','·','... '), Array ('',' & ','" ', ''','-',' <','> ', '·','... '), $ Strcut );
  41. } Else {
  42. $ Dotlen = strlen ($ dot );
  43. $ Maxi = $ length-$ dotlen-1;
  44. $ Current_str = '';
  45. $ Search_arr = array ('&', '', '"', "'",'-',' <','> ', '·','... ', 'Signature ');
  46. $ Replace_arr = array ('&', '', '"', ''', '-', '<', '> ', '·','... ','');
  47. $ Search_flip = array_flip ($ search_arr );
  48. For ($ I = 0; $ I <$ maxi; $ I ++ ){
  49. $ Current_str = ord ($ string [$ I])> 127? $ String [$ I]. $ string [++ $ I]: $ string [$ I];
  50. If (in_array ($ current_str, $ search_arr )){
  51. $ Key = $ search_flip [$ current_str];
  52. $ Current_str = str_replace ($ search_arr [$ key], $ replace_arr [$ key], $ current_str );
  53. }
  54. $ Strcut. = $ current_str;
  55. }
  56. }
  57. Return $ strcut. $ dot;
  58. }

Example 4: Use the strrev () function to flip a character string in PHP. However, sometimes it is necessary to handle strings that contain Chinese characters, so that garbled characters will occur when strrev is used. Here, we define a function to process Chinese characters.

  1. /**

  2. * Chinese string flip
  3. * By bbs.it-home.org
  4. */
  5. Function cstrrev ($ str)
  6. {
  7. $ Len = strlen ($ str );
  8. For ($ I = 0; $ I <$ len; $ I ++)
  9. {
  10. $ Char = $ str {0 };
  11. 'If (ord ($ char)> 127)
  12. {
  13. $ I ++;
  14. If ($ I <$ len)
  15. {
  16. $ Arr [] = substr ($ str, 0, 2 );
  17. $ Str = substr ($ str, 2 );
  18. }
  19. }
  20. Else
  21. {
  22. $ Arr [] = $ char;
  23. $ Str = substr ($ str, 1 );
  24. }
  25. }
  26. Return join (array_reverse ($ arr ));
  27. }

  28. # Usage:

  29. $ Str = 'Chinese. look! ';
  30. Echo cstrrev ($ str );
  31. # Result output :! Kool.

Appendix 5,

  1. Function str_replace_cn ($ needle, $ str, $ haystack, $ charset = "UTF-8 "){
  2. $ Re ['utf-8'] = "/[\ x01-\ x7f] | [\ xc2-\ xdf] [\ x80-\ xbf] | [\ xe0 -\ xef] [\ x80-\ xbf] {2} | [\ xf0-\ xff] [\ x80-\ xbf] {3 }/";
  3. $ Re ['gb2312'] = "/[\ x01-\ x7f] | [\ xb0-\ xf7] [\ xa0-\ xfe]/";
  4. $ Re ['gbk'] = "/[\ x01-\ x7f] | [\ x81-\ xfe] [\ x40-\ xfe]/";
  5. $ Re ['big5'] = "/[\ x01-\ x7f] | [\ x81-\ xfe] ([\ x40-\ x7e] | \ xa1-\ xfe]) /";
  6. Preg_match_all ($ re [$ charset], $ haystack, $ match_haystack );
  7. Preg_match_all ($ re [$ charset], $ needle, $ match_needle );
  8. For ($ I = 0; $ I <count ($ match_needle); $ I ++ ){
  9. If (! In_array ($ match_needle [0] [$ I], $ match_haystack [0]) return $ haystack; // no matching
  10. }
  11. $ Match_haystack = $ match_haystack [0];
  12. $ Match_needle = $ match_needle [0];
  13. For ($ I = 0; $ I <count ($ match_haystack); $ I ++ ){
  14. If ($ match_haystack [$ I] = "") continue;
  15. If ($ match_haystack [$ I] = $ match_needle [0]) {
  16. If (count ($ match_needle) = 1) {// if only one character
  17. $ Match_haystack [$ I] = $ str;
  18. } Else {
  19. $ Flag = true;
  20. For ($ j = 1; $ j <count ($ match_needle); $ j ++ ){
  21. If ($ match_haystack [$ I + $ j]! = $ Match_needle [$ j]) {
  22. $ Flag = false;
  23. Break;
  24. }
  25. }
  26. If ($ flag) {// Match
  27. $ Match_haystack [$ I] = $ str;
  28. For ($ j = 1; $ j <count ($ match_needle); $ j ++ ){
  29. $ Match_haystack [$ I + $ j] = "";
  30. }
  31. }
  32. }
  33. }
  34. }
  35. Return implode ("", $ match_haystack );
  36. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.