The difference and usage of substr () Mb_substr () mb_struct () in PHP and smar..._php tutorial

Source: Internet
Author: User
Tags smarty template
The difference and usage of PHP substr (), Mb_substr () and Mb_strcut and the truncate regulator in Smarty template
PHP substr () function can split the text, but to split the text 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.

As an example:
Echo mb_substr (' This way my string will not have garbled ^_^ ', 0, 7, ' utf-8 ');
?>
Output: so that my word
Echo mb_strcut (' This way my string will not have garbled ^_^ ', 0, 7, ' utf-8 ');
?>
Output: Such a
As can be seen from the above example, MB_SUBSTR is the word to divide the characters, and mb_strcut is to divide the characters by Byte, but will not produce a half-character phenomenon ...

Description of the Mbstring function:

PHP's mbstring Extension module provides multi-byte character processing ability, usually the most commonly used is to use the mbstring to split multi-byte Chinese characters, so as to avoid the occurrence of half a character, because it is the extension of PHP, its performance is better than some custom multi-byte segmentation function.

Mbstring extension provides several functions similar to functions, Mb_substr and mb_strcut, to look at the manuals for explanations of them.

Mb_substr
MB_SUBSTR () returns the portion of STR specified by the start and length parameters.

Mb_substr () performs multi-byte safe substr () operation based on number of characters. Position is counted from the beginning of Str. First character ' s position is 0. Second character position is 1, and so on.

Mb_strcut
Mb_strcut () returns the portion of STR specified by the start and length parameters.

Mb_strcut () performs equivalent operation as MB_SUBSTR () with different method. If start position is multi-byte character ' s second byte or larger, it starts from first byte of multi-byte character.

It subtracts string from str to shorter than length and character that's not part of the multi-byte string or not being middle of shift sequence.

For example, there is a piece of text that uses MB_SUBSTR and mb_strcut to do the slicing:

PLAIN TEXT
CODE:
$str = ' I am a long string of Chinese-www.webjx.com ';

echo "MB_SUBSTR:". Mb_substr ($str, 0, 6, ' utf-8 ');

echo "
";

echo "Mb_strcut:". Mb_strcut ($str, 0, 6, ' utf-8 ');
?>
The output results are as follows:

MB_SUBSTR: I'm a string of comparisons//by character, an English letter, a Chinese character are all characters
Mb_strcut: I am//intercept by byte number, a Chinese character is 3 byte number

Note: When using the Smarty template, it is possible to use the truncate regulator to intercept strings, which can be garbled when there is Chinese in the truncated string (under variable length encoding, the length of the string is usually measured in terms of the number of characters rather than the number of bytes). At this point we can define a variable adjuster by itself to intercept the string by the number of characters. Method: 1. In PHP, the Ord () function obtains the ASCII code of the character 2. Use the substr () function to intercept strings according to ASCII code so there is no garbled language in Chinese.

http://www.bkjia.com/PHPjc/477951.html www.bkjia.com true http://www.bkjia.com/PHPjc/477951.html techarticle the difference and usage of PHP substr (), Mb_substr () and Mb_strcut, and the truncate regulator in Smarty templates. The PHP substr () function can split the text, but the text to be split if included in the text ...

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