PHP Mb_strwidth in the actual use of the problem analysis _php tutorial

Source: Internet
Author: User
A recent PHP Web site project News section needs a feature to intercept the string according to the width of the title and add "..." at the end, the first thought is the use of PHP mb_ string function Implementation, Mb_strimwidth,mb_strwidth, after the discovery if the title exists "" Symbol, PHP Mb_strwidth will think of the symbol is 1 width, I wonder if this is not Chinese double quotation marks, it is supposed to be wide-byte, length should be 2 width, after query "" Unicode respectively is u201c and u201d, not in the range of characters, Then query the unicode.org Code table, found that u2000-u206f is a universal symbol of the range, the characters in this range, although all the form of wide characters, but PHP mb_ function is considered to be 1 width, no way, only on their own.

The following are the implemented functions:

 
 
  1. function truncstring ($STR, $length)
  2. {
  3. $ Countlen = 0 ;
  4. for ($i=0; $i<Mb_strlen($STR); $ i++)
  5. {
  6. $countLen +=amb_strwidth (Mb_substr ($str, $i, 1));
  7. if ($countLen>$length)
  8. Return Mb_substr ($str, 0, $i);
  9. }
  10. return $str;
  11. }
  12. function Amb_strwidth ($str _width)
  13. {
  14. $ Count = 0 ;
  15. for ($i=0; $i<Mb_strlen($ Str_width), $i + +)
  16. {
  17. if (Mb_substr ($str _width, $i, 1) = = "Xe2x80x9c" | | Mb_substr ($str _width, $i, 1) = = ' xe2x80x9d ')
  18. Add 2 to the counter if you encounter characters in u2000-u206f
  19. if (Preg_match ("/[x{2000}-x{206f}]/u", Mb_substr ($str _width, $i, 1)))
  20. $count +=2;
  21. Else
  22. $count +=mb_strwidth (Mb_substr ($str _width, $i, 1));
  23. }
  24. return $count;
  25. }

The above is the use of PHP mb_strwidth when the problem of the specific solution, I hope to play a role in helping people.


http://www.bkjia.com/PHPjc/446292.html www.bkjia.com true http://www.bkjia.com/PHPjc/446292.html techarticle A recent PHP website Project news section needs a feature to intercept the string by the width of the title and add it at the end ..., the first thought is to use PHP mb_ string function Implementation ...

  • 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.