Home >Backend Development >PHP Tutorial >PHP removes the last character of a string and uses substr and mb_substr

PHP removes the last character of a string and uses substr and mb_substr

PHP中文网
PHP中文网Original
2017-06-10 10:13:411731browse

Ordinary string:

Original string: 1,2,3,4,5,6,

This string: 1,2,3,4,5,6, remove the last character ","

Remove the last character ",", the final result is 1,2,3,4,5,6

$str = "1,2,3,4,5,6,";
$newstr = substr($str,0,strlen($str)-1);
echo $newstr;

Chinese character string:

Original string: Wuzhou City

Remove the last Chinese character "city" , the final result is Wuzhou (utf-8 encoding)

$get_city='梧州市';
$get_city=mb_substr($get_city,0,strlen($get_city)-3); 
echo $get_city;

The above introduces the use of PHP to remove the last character of a string with substr and mb_substr, including the content of substr. I hope friends who are interested in PHP tutorials can learn from it. help.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn