PHP substr Library Functions Introduction _php Tutorial

Source: Internet
Author: User
Tags ord
BeginnerThe following PHP substr library function program is not perfect, but the general Chinese (GB18030,GB2312,BIG5) is not a problem. 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<strlen($str)) {
  9. $ output. = $str [$index];
  10. Big5
  11. if (Ord ($str [$index])>=0x81 &&
    Ord ($str [$index])<=0xfe) {
  12. if ($index +1<strlen($str)) {
  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<strlen($str)) {
  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 above code example is the specific use of PHP substr library functions when capturing Chinese characters.


http://www.bkjia.com/PHPjc/446214.html www.bkjia.com true http://www.bkjia.com/PHPjc/446214.html techarticle Beginners The following PHP substr library function program is not perfect, but the general Chinese (GB18030,GB2312,BIG5) is not a problem. This function is not suitable for utf-8 encoded text. $str characters ...

  • Related Article

    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.