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

Source: Internet
Author: User
  1. /**
  2. * Converts a string containing full-width numeric characters, letters, spaces, or '%+-() ' characters to corresponding half-width characters
  3. *
  4. * @access Public
  5. * @param string $str the string to be converted
  6. *
  7. * @return string $STR processed strings
  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 ' and '-d ', ' e ' = ' e ',
  14. ' F ' = ' f ', ' g ' = ' g ', ' h ' = = ' h ', ' i ' = ' I ', ' j ' = ' j ',
  15. ' k ' = ' k ', ' 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 ' = ' = ', ' and ' = ', ' m ' + ' m ', ' n ' = ' n ',
  21. ' O ' = ' ' o ', ' p ' = ' P ', ' q ' = ' Q ', ' r ' = = ' R ', ' s ' = ' and ' s ',
  22. ' t ' = ' t ', ' u ' = ' = ' u ', ' v ' = ' = ' V ', ' w ' = ' W ', ' x ' = ' x ',
  23. ' Y ' = ' y ', ' z ' = ' z ',
  24. ' (' = ' = ' (') ' + ') ', ' (' = ' = ' [', ') ' + '] ', ' ' ' + ', ' [',
  25. ' ' ' = ' and ' ', ' ' = ' = ' [', ' ' ' ' = '] ', ' ' ' ' ' = ' [', ' ' '
  26. ' = ' + ' [', ' ' + '] ', ' {' = ' = ', '} ' = '} ', ' ' = ' < ',
  27. ' ' = ' > ',
  28. '% ' = '% ', ' + ' = ' + ', '-' = '-', '-' = '-', ' ~ ' = '-',
  29. ': ' = ': ', '. ' = = ', ', ', ' = = ', ', ', ' = ', ', ', ' = ', '
  30. '; ' = ', ', '? ' = = '? ', '! ' + '! ', ' ... ' + '-', ' ‖ ' = ' | ',
  31. ' ' ' = ' ', ' ' ' and ' ', ' ' and ' ', ' ' and ' ', ' | '
  32. ' = ' + ', ' $ ' + ' $ ', ' @ ' + ' @ ', ' # ' + ' # ', ' ^ ' + ' ^ ', ' & ' + ' & ', ' * ' = ' * ';
  33. Return Strtr ($str, $arr);
  34. }
Copy Code

Example 2,php Chinese intercept string

  1. /*
  2. Chinese character interception function supported by Utf-8 and gb2312
  3. Cut_str (string, intercept length, start length, encode);
  4. encoding defaults to Utf-8
  5. Start length defaults to 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)) > 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 = "ABCD string to intercept";
  37. Echo Cut_str ($str, 1, 0, ' gb2312 ');
  38. ?>
Copy Code

Example 3, the loading of a string

  1. /**
  2. * Character interception support UTF8/GBK
  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 (' ∵ ', ', ' & ', ' ' ', ' ' ', ' ' ', ' ' ', '-', ' < ', ' > ', ' ' ', ' ' ... '), $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 | | (<= $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 (<= $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 (', ' & ', ' "', ') , ' ' ', ' ' ' ', '-', ' < ', ' > ', ' ' ', ' ... '), $strcut);
  41. } else {
  42. $dotlen = strlen ($dot);
  43. $maxi = $length-$dotlen-1;
  44. $current _str = ";
  45. $search _arr = Array (' & ', ' ', ' ' ', ' ' ', ' ' ', ' ' ', '-', ' < ', ' > ', ' * ', ' ... ', ' ∵ ');
  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. }
Copy Code

Example 4,php Chinese string Flip Flip a string you can use the Strrev () function. However, sometimes it is necessary to deal with the string is a Chinese language, so the use of Strrev will appear garbled. Here you can customize a function to handle characters that contain Chinese.

    1. /**

    2. * Chinese string flipping
    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. #使用方法:

    29. $str = ' Chinese. look! ';
    30. echo Cstrrev ($STR);
    31. #结果输出:!kool. In the text
Copy Code

Attached 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 match
  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. }
Copy Code
  • 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.