php中文支持函数

简介:
Java代码   收藏代码
  1. /** 
  2.  *  将一个字串中含有全角的数字字符、字母、空格或'%+-()'字符转换为相应半角字符 
  3.  * 
  4.  * @access  public 
  5.  * @param   string       $str         待转换字串 
  6.  * 
  7.  * @return  string       $str         处理后字串 
  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.     '”' => '"''’' => '`''‘' => '`''|' => '|''〃' => '"',  
  32.     ' ' => ' ','$'=>'$','@'=>'@','#'=>'#','^'=>'^','&'=>'&','*'=>'*');  
  33.   
  34.     return strtr($str, $arr);  
  35. }  

php中文截取字符串

Java代码   收藏代码
  1. <?php  
  2. /* 
  3. Utf-8、gb2312都支持的汉字截取函数 
  4. cut_str(字符串, 截取长度, 开始长度, 编码); 
  5. 编码默认为 utf-8 
  6. 开始长度默认为 0 
  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需要截取的字符串";  
  37. echo cut_str ( $str, 10'gb2312' );  
  38. ?>   

 

Java代码   收藏代码
  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(' ','&nbsp;''&amp;''&quot;''&#039;''&ldquo;''&rdquo;''&mdash;''&lt;''&gt;''&middot;''&hellip;'), 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(' ', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;'), $strcut);  
  41.     } else {  
  42.         $dotlen = strlen($dot);  
  43.         $maxi = $length - $dotlen - 1;  
  44.         $current_str = '';  
  45.         $search_arr = array('&',' ''"', "'", '', '', '', '<', '>', '·', '','∵');  
  46.         $replace_arr = array('&amp;','&nbsp;''&quot;''&#039;''&ldquo;''&rdquo;''&mdash;''&lt;''&gt;''&middot;''&hellip;',' ');  
  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. }  

PHP中文字符串翻转

通常情况下翻转一个字符串只需要strrev()函数就可以了,但有时我需要处理是字符串是含中文的,这样用strrev就会出现乱码,因此我们需要自定义一个函数来处理含中文的字符。

Java代码   收藏代码
  1. function cstrrev($str)  
  2. {  
  3.     $len = strlen($str);  
  4.     for($i = 0; $i < $len; $i++)  
  5.     {  
  6.         $char = $str{0};  
  7.         if(ord($char) > 127)  
  8.         {  
  9.             $i++;  
  10.             if($i < $len)  
  11.             {  
  12.                 $arr[] = substr($str, 02);  
  13.                 $str = substr($str, 2);  
  14.             }  
  15.         }  
  16.         else  
  17.         {  
  18.             $arr[] = $char;  
  19.             $str = substr($str, 1);  
  20.         }  
  21.     }  
  22.     return join(array_reverse($arr));  
  23. }  
  24. #使用方法:  
  25. $str = '中文.look!';  
  26. echo cstrrev($str);  
  27. #结果输出:!kool.文中  

 str_replace

Java代码   收藏代码
  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.   
  7.         preg_match_all($re[$charset], $haystack, $match_haystack);  
  8.         preg_match_all($re[$charset], $needle, $match_needle);  
  9.   
  10.         for($i = 0; $i < count($match_needle); $i ++){  
  11.             if(!in_array($match_needle[0][$i], $match_haystack[0]))return $haystack;//无匹配  
  12.         }  
  13.   
  14.         $match_haystack = $match_haystack[0];  
  15.         $match_needle = $match_needle[0];  
  16.   
  17.         for($i = 0; $i < count($match_haystack); $i ++){  
  18.             if($match_haystack[$i] == "")continue;  
  19.             if($match_haystack[$i] == $match_needle[0]){  
  20.                 if(count($match_needle) == 1){//如果只一个字符  
  21.                     $match_haystack[$i] = $str;  
  22.                 }else{  
  23.                     $flag = true;  
  24.                     for($j = 1; $j < count($match_needle); $j ++){  
  25.                         if($match_haystack[$i + $j] != $match_needle[$j]){  
  26.                             $flag = false;  
  27.                             break;  
  28.                         }  
  29.                     }  
  30.                     if($flag){//匹配  
  31.                         $match_haystack[$i] = $str;  
  32.                         for($j = 1; $j < count($match_needle); $j ++){  
  33.                             $match_haystack[$i + $j] = "";  
  34.                         }  
  35.                     }  
  36.                 }  
  37.             }  
  38.         }  
  39.         return implode("", $match_haystack);  
  40.     }  

 

Java代码   收藏代码
  1. /** 
  2.  * 实现多种字符编码方式 
  3.  * @param $input 需要编码的字符串 
  4.  * @param $_output_charset 输出的编码格式 
  5.  * @param $_input_charset 输入的编码格式 
  6.  * return 编码后的字符串 
  7.  */  
  8. function charsetEncode($input, $_output_charset, $_input_charset) {  
  9.     $output = "";  
  10.     if (!isset($_output_charset)) $_output_charset = $_input_charset;  
  11.     if ($_input_charset == $_output_charset || $input == null) {  
  12.         $output = $input;  
  13.     } elseif (function_exists("mb_convert_encoding")) {  
  14.         $output = mb_convert_encoding($input, $_output_charset, $_input_charset);  
  15.     } elseif (function_exists("iconv")) {  
  16.         $output = iconv($_input_charset, $_output_charset, $input);  
  17.     } else die("sorry, you have no libs support for charset change.");  
  18.     return $output;  
  19. }  
  20.   
  21. /** 
  22.  * 实现多种字符解码方式 
  23.  * @param $input 需要解码的字符串 
  24.  * @param $_output_charset 输出的解码格式 
  25.  * @param $_input_charset 输入的解码格式 
  26.  * return 解码后的字符串 
  27.  */  
  28. function charsetDecode($input, $_input_charset, $_output_charset) {  
  29.     $output = "";  
  30.     if (!isset($_input_charset)) $_input_charset = $_input_charset;  
  31.     if ($_input_charset == $_output_charset || $input == null) {  
  32.         $output = $input;  
  33.     } elseif (function_exists("mb_convert_encoding")) {  
  34.         $output = mb_convert_encoding($input, $_output_charset, $_input_charset);  
  35.     } elseif (function_exists("iconv")) {  
  36.         $output = iconv($_input_charset, $_output_charset, $input);  
  37.     } else die("sorry, you have no libs support for charset changes.");  
  38.     return $output;  
  39. }  

 sfds

相关文章
|
5月前
|
Shell PHP Windows
PHP代码审计(四)PHP文件操作函数(2)
改变文件所有者。如果成功则返回 TRUE,如果失败则返回 FALSE。 语法:chown(file,owner)
39 0
|
5月前
|
安全 Unix Shell
PHP代码审计(四)PHP文件操作函数(1)
改变文件所有者。如果成功则返回 TRUE,如果失败则返回 FALSE。 语法:chown(file,owner)
39 0
|
5月前
|
小程序 PHP 数据安全/隐私保护
php图片加水印函数
这里分享下php给图片加水印的几个自定义函数 给图片加水印首先需要开启GD库。 用到的php函数是imagecopymerge () 和 imagecopy () imagecopymerge 函数可以支持两个图像叠加时,设置叠加的透明度
48 0
|
8月前
|
PHP
PHP 常用系统函数
PHP 常用系统函数
40 0
|
11天前
|
PHP Perl
PHP-ereg()函数
但再次强调,不建议使用ereg()函数,而应该改用preg_match()函数来执行更灵活和强大的正则表达式匹配操作。
19 4
|
14天前
|
存储 PHP 数据库
【PHP开发专栏】PHP数组操作与常见函数
【4月更文挑战第29天】本文介绍了PHP中的数组及其操作,包括定义与初始化、访问与修改、遍历。还探讨了常用的数组函数,如count()、in_array()、array_push/pop()、array_shift/unshift()、array_merge()、array_keys/values()以及sort()和rsort(),帮助开发者更有效地处理和操作数组。
|
15天前
|
运维 JavaScript Serverless
Serverless 应用引擎产品使用之在阿里函数计算中,Php环境,配置取消禁止函数exec如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
32 4
|
1月前
|
JSON PHP 数据格式
php函数
php函数
8 0
|
1月前
|
PHP
php事务删除加调用日志函数
php事务删除加调用日志函数
8 1
|
3月前
|
PHP
从建站到拿站 -- PHP判断循环及函数
从建站到拿站 -- PHP判断循环及函数
14 0