Analysis of the Usage Example of php String Interception Function mb_substr

  • 2021-12-12 04:07:37
  • OfStack

This article illustrates the use of the php string interception function mb_substr. Share it for your reference, as follows:

string mb_substr (string $str, int $start [, int $length = NULL [, string $encoding = mb_internal_encoding ()]])-Intercept string

(PHP 4 > = 4.0.6, PHP 5)

$str the target string to get the string (string starts at 0)
The position of the first character to use in $start, $str
$length, the length of the substring obtained (note that it is not the end position)
$encoding, you can specify the character encoding (1 is generally used when processing Chinese characters, and this problem is encountered very much)

Example:


<?php
mb_internal_encoding ("UTF-8");  // If you put UTF-8 Change to coding , The following values for Chinese string processing will change. 
echo mb_internal_encoding()."";  // Gets the character encoding as ISO-8859-1
echo mb_substr('abcdefghijk',0,9)."";  //abcdefghi
echo mb_substr('abcdefghijk',1,5)."";  //bcdef
echo mb_substr(' We are all Chinese ',0,9)."";  // We are all Chinese 
echo mb_substr(' We are all Chinese ',0,9,'gb2312')."";  // We are all Chinese 
echo mb_substr(' We are all Chinese ',0,9,'utf-8');  // We are all Chinese 
?>

Summary:

1. When dealing with English strings, the fourth parameter ($encoding) of this function can be ignored.
2. Be careful when dealing with Chinese strings. 1. Consider coding problems. Different coding leads to different Chinese values.
3. When Chinese strings are imported into the database, it is even more important.
4. The coding ability to deal with strings or texts is a standard to measure programmers' skills.

Supplement: Differences between substr and mb_substr

Similarities and differences:

Both functions intercept the length of the string, the difference is that mb_substr It can be used to intercept multi-byte encoded Chinese characters, so as to prevent the appearance of garbled codes

Example description:

Example 1:


<?php
$chuan=" I'm Chinese !";
echo substr($chuan,0,3);
echo '<br />';
echo mb_substr($chuan,0,3,'utf8');
?>

Run results:

I
I am a middle school student

Under utf8 encoding, because one Chinese character occupies three bytes, the first output is: I
The second output is: I am in

Example 2:


<?php
$chuan=" I'm Chinese !";
echo substr($chuan,0,-4);
echo '<br />';
echo mb_substr($chuan,0,-2,'utf8');
?>

Run results:

I'm China
I'm China

Both outputs are: I am China
If the first length is minus 4, it will be omitted! And people, starting from scratch, will include me as China
The second one is the same

For more readers interested in PHP related contents, please check the special topics of this site: "Summary of Common Functions and Skills of php", "Summary of Usage of php String (string)", "Encyclopedia of Operation Skills of PHP Array (Array)", "Introduction to Basic Grammar of PHP", "Introduction to Database Operation of php+mysql" and "Summary of Operation Skills of Common Database of php"

I hope this paper is helpful to everyone's PHP programming.


Related articles: