Home >Backend Development >PHP Problem >How to delete the last digit of a string in php

How to delete the last digit of a string in php

coldplay.xixi
coldplay.xixiOriginal
2020-10-07 12:01:274115browse

php method to delete the last digit of a string: 1. Use the [substr()] function, the code is [substr($arr_str,0,strlen($arr_str)-1)]; 2. Use [ rtrim()] function, the code is [rtrim($arr_str, ",")].

How to delete the last digit of a string in php

php method to delete the last digit of the string:

Method one:

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

Detailed explanation: Substr () Function grammar: String Substr (String $ String, Int $ Start [, Int $ LENGTH])

# stelen () Function: int strlen (string $string)

Principle of this example:

First use the strlen() function to determine the length of the string

$arr_str, and then use the substr() function to $ Intercept arr_str 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 three:

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:

This is just for this Need to prepare!

Note: After the above method operates on the string, it returns the operation result and does not change the string itself! Remember to use a variable to receive the result!

Related free learning recommendations:

php programming (video)

The above is the detailed content of How to delete the last digit of a string in php. 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