Home  >  Article  >  Backend Development  >  How to remove the last character from a php string

How to remove the last character from a php string

青灯夜游
青灯夜游Original
2021-03-12 17:30:254671browse

Removal method: 1. Use substr() and strlen() functions to remove, syntax "substr($arr_str,0,strlen($arr_str)-1)"; 2. Use substr() function to remove directly , the syntax is "substr($arr_str, 0, -1)"; 3. Use the rtrim() function to remove, the syntax is "rtrim($arr_str, ",")".

How to remove the last character from a php string

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

Delete the last digit in PHP Summary of character methods:

Method 1: substr($arr_str,0,strlen($arr_str)-1);

Detailed explanation:

substr() function syntax: string substr ( string $string , int $start [, int $length ] )

strlen() function syntax: int strlen (string $string)

Principle of this example:

First use the strlen() function to get the length of the string $arr_str, and then use the substr() function to modify $arr_str Intercept, intercept to the penultimate digit of $arr_str. This removes the last ",".

Usage experience:

Not recommended, there is a simpler and more useful way in PHP!

Method 2: substr($arr_str, 0, -1)

Detailed explanation: directly use the substr() function to cut off the last character in reverse order;

Usage experience: It is still very suitable~~However, first you have to make sure that there must be content in the string, and the last digit must not be included!

Method 3: rtrim($arr_str, ",")

Detailed explanation:

rtrim() function syntax: string rtrim ( string $str [, string $character_mask ] )

rtrim — Delete the blank characters (or other characters) at the end of the string

Usage experience:

It’s simply Prepared for this need!

[Recommended learning: "PHP Video Tutorial"]

The above is the detailed content of How to remove the last character from a php string. For more information, please follow other related articles on the PHP Chinese website!

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