Home >Backend Development >PHP Problem >How to intercept string in php
php has a large number of built-in string manipulation functions, such as php implode, explode, etc. To intercept strings in php, you can use the substr and mb_substr functions.
phpsubstr syntax
substr(string, start, length)
Parameters
string 即要截取的字符串 start 即要截取的开始位置(0表示从从前往后数 第一个字符开始,负数表示从从后往前数) lengthlength 当为正数时,为需要截取的长度;当为负数时,即理解为去掉末尾的几个字符
For example
$str1 = substr("abcdef", 1); // 返回 "bcdef" $str2 = substr("abcdef", 2); // 返回 "cdef" $str3 = substr("abcdef", 0,1); // 返回 "a" $str4 = substr("abcdef", 0,2); // 返回 "ab" $str5 = substr("abcdef", -1); // 返回 "f" $str6 = substr("abcdef", -2); // 返回 "ef" $str7 = substr("abcdef", 0,-1); // 返回 "abcde" $str8 = substr("abcdef", 0,-2); // 返回 "abcd" $str9 = substr('你好你好你好',1);//返回:??好你好你好 $str10 = mb_substr('你好你好你好',1);//返回:好你好你好
Careful children may have discovered that substr in $str9 returns � �Hello hello hello, there are Chinese garbled characters appearing.
Substr is often used in PHP to intercept strings, but when we use it to intercept Chinese characters, garbled characters will occur. At this time, we need to use another
One function, mb_substr, now the problem of garbled characters is solved.
The above is the detailed content of How to intercept string in php. For more information, please follow other related articles on the PHP Chinese website!