I'm trying to remove the first and last chars of a string. I'm experiencing weirdness.
Check it out:
$string = 'This is a test.';
$string = mb_substr($string, 1, mb_strlen($string) - 1);
var_dump($string);
Output:
string(14) "his is a test."
And then this:
$string = 'This is a test.';
$string = mb_substr($string, 1, mb_strlen($string) - 2);
var_dump($string);
Output:
string(13) "his is a test"
The length of the string minus 1 should be enough, but I have to go TWO minus. Why?
Because your string This is a test.
length is 15 .
To get new string without last and first symbols - you need 13 symbols, which is exactly mb_strlen($string) - 2
.
Also, third argument of mb_substr
is not position of last considered symbol, it is length of the substring that you want to receive.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.