Grammar:
String substr (string $string, int $start [, int $length]), which can be used to find a matching string or character in a longer string, $string the string to be processed, $start the position to start the selection, $length is the length to select.
$length read characters from left to right for positive data.
When the $length is negative, the characters are read right-to-left.
String required, which specifies the strings to return a portion of.
Start is required to specify where the string begins.
Charlist Optional, which specifies the length of the string to return, by default until the end of the string.
Positive number-starts at the specified position in the string
Negative number-starts at the specified position from the end of the string
0-Starts at the first character in a string
The PHP instance code is as follows:
$rest _1 = substr ("abcdef", 2); Returns "Cdef" $rest _2 = substr ("ABCdef",-2); Returns "EF" $rest 1 = substr ("abcdef", 0, 0);//Returns "" $rest 2 = substr ("abcdef", 0, 2); Returns "AB" $rest 3 = substr ("abcdef", 0,-1); Returns "ABCDE" $rest 4 = substr ("abcdef", 2,0); Returns "" $rest 5 = substr ("abcdef", 2,2); Returns "CD" $rest 6 = substr ("ABCdef", 2,-1); Returns "CDE" $rest 7 = substr ("abcdef", -2,0); Returns "" $rest 8 = substr ("abcdef", -2,2); Returns "EF" $rest 9 = substr ("abcdef", -2,-1); Returns "E"
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 encoding of the string, but the general server does not open Php_mbstring.dll, you need to open the Php_mbstring.dll in php.ini.
<?phpecho Mb_substr (' We are all good children hehe ', 0,9);? >
Output: We all
Now we add the character set Utf-8
<?phpecho Mb_substr (' We are all good children hehe ', 0,9, ' utf-8 ');? >
Output: We're all good kids he
The first one is in three bytes for a Chinese, this is the characteristics of utf-8 encoding, followed by the Utf-8 character set description, so, is in a word to intercept the
The following is a function that intercepts UTF-8 encoded strings within Ecshop
function sub_str ($str, $length = 0, $append = True) {$str = Trim ($STR); $strlength = strlen ($STR); if ($length = = 0 | | $length >= $strlength) {return $str; Intercept length equal to 0 or greater than equal to the length of this string, return the string itself} elseif ($length < 0)//If the intercept length is negative {$length = $strlength + $length;//Then The Intercept length is equal to the length of the string minus the intercept length if ($length < 0) {$length = $strlength;//If the absolute value of the intercept length is greater than the length of the string itself, the length of the string itself is truncated. Degrees}} if (Function_exists (' mb_substr ')) {$newstr = Mb_substr ($str, 0, $length, ec_charset); } elseif (Function_exists (' iconv_substr ')) {$newstr = Iconv_substr ($str, 0, $length, ec_charset); } else {//$newstr = Trim_right (substr ($str, 0, $length)); $newstr = substr ($str, 0, $length); if ($append && $str! = $newstr) {$newstr. = ' ... '; } return $newstr;}