php function Mb_strcut Use tips to share _php tutorials

Source: Internet
Author: User
We are doingPHP function Mb_strcut, their two usage is similar to substr (), but in the PHP function mb_strcut the last to add a parameter to set the string encoding, but the general server is not open php_ Mbstring.dll, need to php.ini in the Php_mbstring.dll to open, if we are not sure whether to open this function, then we'd better first to judge, if (Function_exist (mb_string)).

As an example:

 
  
  
  1. <? PHP
  2. echo mb_substr (' So my string
    There will be no garbled ^_^ ', 0, 7, ' utf-8 ');
  3. ?>

Output: so that my word

Example of PHP function mb_strcut

 
  
  
  1. <? PHP
  2. Echo mb_strcut (' This way my string will not have garbled ^_^ ', 0, 7, ' utf-8 ');
  3. ?>

Output: Such a

As can be seen from the above example, MB_SUBSTR is the word to divide the characters, and PHP function mb_strcut is to divide the characters by Byte, but will not produce a half character phenomenon, but sometimes in the output will also appear when the interception of something is incorrect, some right some wrong, And the Chinese also garbled, the main reason for these phenomena is that the way you make this function is inconsistent with the way you display the encoding of his web page.
A custom function that PHP intercepts Chinese strings is also described here:

A method of text string interception without garbled characters in PHP implementation

 
 
  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 is garbled after the use of PHP function mb_strcut The main steps to solve the problem.


http://www.bkjia.com/PHPjc/446298.html www.bkjia.com true http://www.bkjia.com/PHPjc/446298.html techarticle we are in the PHP function Mb_strcut, their usage is similar to substr (), but in the PHP function mb_strcut the last to add a parameter to set the string encoding, but the general ...

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