Home >Backend Development >PHP Tutorial >Double-byte character truncation

Double-byte character truncation

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-25 09:11:30933browse
Double-byte character truncation, such as truncation of mixed Chinese and English text, etc.
  1. function substr($string, $length)
  2. {
  3. $i = 0;
  4. $j = 0;
  5. while($i < $length)
  6. {
  7. $char = ord($string[$ j]);
  8. if($char <= 0x7F)
  9. {
  10. $j++;
  11. }
  12. else
  13. {
  14. $j += 2;
  15. }
  16. $i++;
  17. }
  18. return substr($string,0, $j);
  19. }
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