PHPSubstr library function introduction _ PHP Tutorial

Source: Internet
Author: User
Function introduction of PHPSubstr Library. The PHPSubstr library function program below is not perfect for beginners, but there is no problem in handling general Chinese (GB18030, GB2312, BIG5. This function is not suitable for UTF-8 encoded text. $ StrThe following PHP Substr library function program is not perfect, but it is no problem to process General Chinese (GB18030, GB2312, BIG5. This function is not suitable for UTF-8 encoded text.

 
 
  1. // $ Str string
  2. // $ Max maximum number of characters
  3. Function Substring ($ str, $ max ){
  4. $ Cnt = 0; // actual count
  5. $ Index = 0; // Current index
  6. $ Output = ''; // output
  7. //
  8. While ($ cnt <$ max & $ index
  9. $ Output. = $ str [$ index];
  10. // Big5
  11. If (ord ($ str [$ index])> = 0x81 &&
    Ord ($ str [$ index]) <= 0xfe ){
  12. If ($ index + 1
  13. If (ord ($ str [$ index + 1])> = 0x40
    & Ord ($ str [$ index + 1]) <0x7e)
  14. | (Ord ($ str [$ index + 1])> = 0xa1
    & Ord ($ str [$ index + 1]) <= 0xfe )){
  15. $ Index ++;
  16. $ Output. = $ str [$ index];
  17. }
  18. }
  19. }
  20. // Gb2312
  21. Else if (ord ($ str [$ index])> = 0xa1
    & Ord ($ str [$ index]) <= 0xf7 ){
  22. $ Output. = $ str [$ index];
  23. If ($ index + 1
  24. If (ord ($ str [$ index + 1])> = 0xa1
    & Ord ($ str [$ index + 1]) <0xfe ){
  25. $ Index ++;
  26. $ Output. = $ str [$ index];
  27. }
  28. }
  29. }
  30. Else {
  31. }
  32. $ Cnt ++;
  33. $ Index ++;
  34. }
  35. Return $ output;
  36. }

The preceding code example shows how to use the PHP Substr library function to intercept Chinese characters.


The PHP Substr library function program below explain is not perfect, but there is no problem in handling general Chinese (GB18030, GB2312, BIG5. This function is not suitable for UTF-8 encoded text. // $ Str character...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.