Example explanation of the commonly used string acquisition function mb_strcut in php

string mb_strcut ( string $str , int $start [, int $length = NULL [, string $encoding = mb_internal_encoding() ]] ) – get part of a character
1, (PHP 4 >= 4.0.6, PHP 5)
2, $str The target string of the string to be obtained (the starting position of the string is 0)
3. $start, the starting position, in bytes. (must pay attention to bytes)
4, $length, byte length (must pay attention to bytes)
5, $encoding, can specify the character encoding (usually used when dealing with Chinese characters, and this problem encountered very 6. Special attention: Chinese characters occupy 3 bytes under UTF-8 encoding and
2 bytes under GB2312 encoding, so there are garbled characters in the following examples

<?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_strcut('abcdefghijk',0,9)."";	//abcdefghi
echo mb_strcut('abcdefghijk',1,5)."";	//bcdef
echo mb_strcut('We are all Chinese',0,9).""; //We are all Chinese
echo mb_strcut('We are all Chinese',0,9,'gb2312').""; //There are garbled characters here
echo mb_strcut('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. It is even more important when Chinese strings operate database access.
4. The coding ability of processing strings or texts is a standard for measuring the skill of programmers.

Similar to this function, mb_substr() , please refer to it directly.

 

Guess you like

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