PHP substr intercept Chinese characters garbled problem troubleshoot _php tutorial

Source: Internet
Author: User
We are using1, the use of mbstring Extension Library MB_SUBSTR interception will not appear garbled.

2, write the interception function, but the efficiency is not as high as the Mbstring expansion library.

3, if only for the output interception of the string, can be implemented as follows: substr ($STR, 0,). chr (0).

SUBSTR () function can split the text, but the text to be divided if the inclusion of Chinese characters tend to encounter problems, you can use MB_SUBSTR ()/mb_strcut This function, Mb_substr ()/mb_strcut usage and substr () similar, Just in Mb_substr ()/mb_strcut to add more than one parameter to set the string encoding, but the general server did not open Php_mbstring.dll, need to php.ini in the Php_mbstring.dll to open.

For an example of PHP substr intercept Chinese characters:

 
  
  
  1. < ? PHP
  2. Echo mb_substr (' This way my string will not have garbled ^_^ ', 0, 7, ' utf-8 ');
  3. ?>
  4. Output: so that my word
  5. < ? PHP
  6. Echo mb_strcut (' This way my string will not have garbled ^_^ ', 0, 7, ' utf-8 ');
  7. ?>

Output: Such a
As can be seen from the above example, Mb_substr is to divide characters by word, while mb_strcut splits characters by Byte, but does not produce a half-character phenomenon.

The method of PHP substr intercept Chinese characters without garbled characters

 
 
  1. function Gbsubstr ($string, $start, $length) {
  2. if (strlen ($string)>$length) {
  3. $ Str = NULL ;
  4. $ Len = $start + $length;
  5. For ($i= $start; $i<$len; $i + +) {
  6. if (Ord (substr ($string, $i, 1))>0xa0) {
  7. $ str. = substr ($string, $i, 2);
  8. $i + +;
  9. }else{
  10. $ str. = substr ($string, $i, 1);
  11. }
  12. }
  13. return $str. ' ... ';
  14. }else{
  15. return $string;
  16. }
  17. }

The above two code examples are PHP substr intercept Chinese characters when the reason for garbled and the solution is introduced.


http://www.bkjia.com/PHPjc/446215.html www.bkjia.com true http://www.bkjia.com/PHPjc/446215.html techarticle we are using 1, using the mbstring extension Library MB_SUBSTR interception will not appear garbled. 2, write the interception function, but the efficiency is not as high as the Mbstring expansion library. 3. If it is only ...

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