首頁  >  文章  >  後端開發  >  php中文支援函數 php中文截取字串函數 PHP中文字串翻轉

php中文支援函數 php中文截取字串函數 PHP中文字串翻轉

WBOY
WBOY原創
2016-07-25 08:57:34906瀏覽
  1. /**
  2. * 將一個字串中含有全角的數字字元、字母、空格或'%+-()'字元轉換為對應半角字元
  3. *
  4. * @access public
  5. * @param string $str 待轉換字串
  6. *
  7. * @return string $str 處理後字串
  8. */
  9. function make_semiangle($str)
  10. {
  11. $arr = 隊列( '0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4' ,
  12. '5 ' => '6', '7' => '8', '9' => '9',
  13. 'A' => 'A', 'B' => 'B', 'C' = > > 'D', 'E' => 'E',
  14. 'F', 'G' => 'H', 'I' => 'J' => 'K', 'L' => ' M', 'N' => 'N', 'O' => 'O',
  15. 'P', 'Q' => 'Q', 'R' => > 'S', 'T ' => 'U' => 'V', 'W' => X' => 'X', 'Y' => 'Y',
  16. 'Z' => 'a', 'b ' =>; ', 'c' => 'c', 'd' => 'd',
  17. 'e' => 'e', 'f' =>; > 'i',
  18. 'j', 'k' =>; ' => 'l', 'm' => 'n',
  19. 'o', 'p' =>, 'q ' => 'q', 'r ' => 'r', 's' => 's',
  20. 't' =>; 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
  21. 'y' =>; 'y', 'z' => 'z',
  22. ' (' =>; '(', ')' => ')', '〔' => '[', ']' => ']', '【' => '[',
  23. '】 ' => ']', '〖 ' => '[', '〗' => ']', '“' => '[', '”' => ']',
  24. ''' = > '[', ''' = > ']', '{' => '{', '}' =>; '}', '《' => ' '》' => '>',
  25. '% ' =>; '+' => '+', '—' => '-', '-' => '-', '~' => ' -',
  26. ':' =>; ' :', '。 , '? ', ''' => '`', ''' => '`', '|' => '|', '〃' => '"' ,
  27. '' => ' ','$ '=>'$','@'=>'@','#'=>'#','^'=>'^','&' =>'&','*'=>'* ');
  28. return strtr($str, $arr);
  29. }
  30. 複製程式碼
例2,php中文截取字串

/*
    Utf-8、gb2312都支援的漢字截取函數
  1. cut_str(, 截取長度字串、開始長度、編碼);
  2. 編碼為預設utf-8
  3. 開始長度預設為0
  4. * edit: bbs.it-home.org
  5. */
  6. function cut_str($string , $sublen, $start = 0, $code = 'UTF-8') {
  7. if ($code == 'UTF-8') {
  8. $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]/";
  9. preg_match_all ( $pa, $string, $t_string );
  10. if (count (count (count. $t_string [0] ) - $start > $sublen)
  11. return join ( '', array_slice ( $t_string [0], $start, $sublen ) ) 。 「…」;
  12. return join ( ' ', array_slice ( $t_string [0], $start, $sublen ) );
  13. } else {
  14. $start = $start * 2;
  15. $sublen = $sublen *
  16. $; strlen = strlen ( $string );
  17. $tmpstr = ''
  18. for($i = 0; $i if ($i >= $start && $ i if (ord ( substr ( $string, $i, 1 ) ) > 129) {
  19. $tmpstr .= substr ( $string, $i, 2 ) ;
  20. } else {
  21. $tmpstr .= substr ( $string, $i, 1 );
  22. }
  23. }
  24. if (ord ( substr ( $string, $i, 1 ) ) > 129)
  25. $i ++;
  26. }
  27. if (strlen ( $tmpstr ) $tmpstr .= "...";
  28. 回傳 $tmpstr;
  29. }
  30. }
  31. $str = "abcd 需要截取的字串";
  32. echo cut_str ( $str, 1, 0, 'gb2312' );
  33. 例3,字符串的载取

    1. /**
    2. * 字符截取 支持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(' ',' ', '&', '"', ''', '“', '”', '—', '<', '>', '·', '…'), 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 || (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('∵', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), 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. }
    复制代码

    例4,PHP中文字符串翻转 翻转一个字符串可以使用strrev()函数即可。 但有时需要处理是字符串是含中文的,这样用strrev就会出现乱码。 这里自定义一个函数来处理含中文的字符。

    1. /**

    2. * 中文字符串翻转
    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 = '中文.look!';
    30. echo cstrrev($str);
    31. #结果输出:!kool.文中
    复制代码

    附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 if(!in_array($match_needle[0][$i], $match_haystack[ 0 ]))return $haystack;//無符合
    9. }
    10. $match_haystack = $match_haystack[0];
    11. $match_needle = $match_needle[0];
    12. for($i = 0; $i if($match_haystack[$i] == "")繼續;
    13. if($match_haystack[$i] == $match_needle[0]){
    14. if(count($match_needle) == 1){//若只有一個字元
    15. $match_haystack[$i] = $str;
    16. }else{
    17. $flag = true;
    18. for($j = 1; $j if($match_haystack[$i + $j] != $match_needle[$j]){
    19. $flag = false;
    20. 休息;
    21. }
    22. }
    23. if($flag){//符合
    24. $match_haystack[$i] = $str;
    25. for($j = 1; $j $match_haystack[$i + $j] = "";
    26. }
    27. }
    28. }
    29. }
    30. }
    31. return implode("", $match_haystack);
    32. }
    複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn