Example explanation of the commonly used string interception function mb_substr in php

string mb_substr ( string $str , int $start [, int $length = NULL [, string $encoding = mb_internal_encoding() ]] )-截取字符串

(PHP 4 >= 4.0.6, PHP 5)
$str To get the target string of the string (the starting position of the string is 0)
$start, the position of the first character to be used in $str
$length, get The length of the substring (note that it is not the end position)
$encoding, you can specify the character encoding (generally used when dealing with Chinese characters, and this problem is encountered a lot)

<?php
mb_internal_encoding ("UTF-8"); //If UTF-8 is changed to encoding, the following values ​​for Chinese string processing will change.
echo mb_internal_encoding().""; //Get 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
?>

 Summarize:

1. When processing English strings, the fourth parameter ($encoding) of this function can be ignored.
2. Be careful when dealing with Chinese strings. Be sure to consider the encoding problem. Different encodings have different Chinese values.
3. When Chinese strings are imported into the database, it becomes more important.
4. The coding ability of processing strings or texts is a standard for measuring the skill of programmers.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326912103&siteId=291194637