Home Backend Development PHP Tutorial PHP Chinese support function PHP Chinese interception string function PHP Chinese string flip

PHP Chinese support function PHP Chinese interception string function PHP Chinese string flip

Jul 25, 2016 am 08:57 AM

  1. /**
  2. * Convert a string containing full-width numeric characters, letters, spaces or '%+-()' characters into corresponding half-width characters
  3. *
  4. * @access public
  5. * @param string $str String to be converted
  6. *
  7. * @return string $str processed string
  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. return strtr($str, $arr);
  34. }
复制代码

例2,php中文截取字符串

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

例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 < count($match_needle); $i ++){
  9. if(!in_array($match_needle[0][$i], $match_haystack[0]))return $haystack;//无匹配
  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){//如果只一个字符
  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){//匹配
  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. }
复制代码


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles