Home >Backend Development >PHP Tutorial >PHP intercepts Chinese and English without garbled characters, including full-width characters

PHP intercepts Chinese and English without garbled characters, including full-width characters

WBOY
WBOYOriginal
2016-07-25 09:01:181043browse
Code source: Xiaomo (self)
Complies with UTF-8, if it is under GBK
Change to $content .= $str[$sing].$str[$sing+1];
$sing += 3; changed to $sing += 2;
  1. /**
  2. * Chinese and English interception
  3. * @param string The string to be intercepted
  4. * @param string The length to be intercepted (exceeding the total length will be calculated as the total length)
  5. * @param [string] (optional) starting position (the first one is 0)
  6. * @return string
  7. * @author Xiaomo 244349067@qq.com
  8. */
  9. function mixSubstr($str, $length, $start=FALSE)
  10. {
  11. if( ! $length){
  12. return false;
  13. }
  14. $strlen = strlen( $str);
  15. $content = '';
  16. $sing = 0;
  17. $count = 0;
  18. if($length > $strlen) {
  19. $length = $strlen;
  20. }
  21. if($start > ;= $strlen) {
  22. return false;
  23. }
  24. while($length != ($count-$start))
  25. {
  26. if(ord($str[$sing]) > 0xa0) {
  27. if( !$start || $start <= $count) {
  28. $content .= $str[$sing].$str[$sing+1].$str[$sing+2];
  29. }
  30. $sing + = 3;
  31. $count++;
  32. }else{
  33. if(!$start || $start <= $count) {
  34. $content .= $str[$sing];
  35. }
  36. $sing++;
  37. $count++;
  38. }
  39. }
  40. return $content;
  41. }
Copy code


Statement:
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