Differences and usage of substr () mb_substr () mb_struct () in PHP and Smar...

Source: Internet
Author: User
Tags smarty template

Differences and usage of PHP substr (), mb_substr () and mb_strcut and the introduction of the truncate regulator in the Smarty Template
The PHP substr () function can split text. However, if the text to be split contains Chinese characters, the function mb_substr ()/mb_strcut can be used () the usage of/mb_strcut is similar to that of substr (), but an additional parameter must be added at the end of mb_substr ()/mb_strcut to set the character string encoding. However, php_mbstring.dll is not enabled on the server, in php. ini opens php_mbstring.dll.

For example:
<? Php
Echo mb_substr ('in this way, my strings will not contain garbled characters such as ^_^', 0, 7, 'utf-8 ');
?>
Output: in this way, my words
<? Php
Echo mb_strcut ('in this way, my strings will not contain garbled characters such as ^_^', 0, 7, 'utf-8 ');
?>
Output:
From the above example, we can see that mb_substr is a word-based splitting character, while mb_strcut is a byte-based splitting character, but it does not produce a half character ......

Description of the mbstring function:

The mbstring extension module of php provides the processing capability of Multi-byte characters. The most common feature is to use mbstring to split multi-byte Chinese characters, which can avoid the occurrence of half a character, because it is an extension of php, its performance is better than some custom multibyte splitting functions.

Mbstring extension provides several function-like functions, mb_substr and mb_strcut. Let's take a look at their explanations in the manual.

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 that is shorter than length AND character that is not part of multi-byte string or not being middle of shift sequence.

For another example, we use mb_substr and mb_strcut to split a piece of text:

PLAIN TEXT
CODE:
<? Php
$ Str = 'I am a long string of Chinese characters -www.webjx.com ';

Echo "mb_substr:". mb_substr ($ str, 0, 6, 'utf-8 ');

Echo "<br> ";

Echo "mb_strcut:". mb_strcut ($ str, 0, 6, 'utf-8 ');
?>
The output result is as follows:

Mb_substr
Mb_strcut: I am a // The number of bytes. One Chinese character is 3 bytes.

Note: The truncate regulator may be used to intercept strings when using the smarty template. At this time, garbled characters will occur when the intercepted strings contain Chinese characters (in the case of variable-length encoding, generally, the length of a string is measured by the number of characters rather than the number of bytes. In this case, we can define a variable regulator to intercept a string based on the number of characters. Method: 1. Use the ord () function in php to obtain the character ascii code. 2. Use the substr () function to intercept the character string based on the ascii code, so that there is no Garbled text.

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.