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中文网
Release: 2023-02-28 21:52:02
Original
1724 people have browsed it

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;
Copy after login

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;
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template