Home > Backend Development > PHP Problem > How to delete the last character in php

How to delete the last character in php

王林
Release: 2023-03-06 19:34:01
Original
2577 people have browsed it

How to delete the last character in php: You can use the rtrim() function, such as [rtrim ($text, ",");]. The rtrim() function can remove blank characters or other characters at the end of a string.

How to delete the last character in php

Method 1:

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

( Recommended tutorial: php video tutorial)

Description:

string rtrim ( string $str [, string $character_mask ] )
Copy after login

This function deletes the blank characters at the end of str and returns.

Without the second parameter, rtrim() only removes the following characters:

How to delete the last character in php

##$str input string.

$ character_mask By specifying character_mask, you can specify the character list you want to delete. Simply list all the characters you want to remove. Using the .. format, a range can be specified.

Example:

$text = 'b,c,d,';
$trimmed  =  rtrim ( $text ,  "," );
 var_dump ( $trimmed );
Copy after login

Output

b,c,d
Copy after login

Method 2:

substr returns the substring of the string


string substr ( string $string , int $start [, int $length ] )
Copy after login

$ string The string to be processed

$start The starting position

If start is a non-negative number, the returned string will start from the start position of string and start counting from 0. For example, in the string "abcdef", the character at position 0 is "a", the character at position 2 is "c", and so on.

If start is a negative number, the returned string will start from the start character forward from the end of string.

If the length of string is less than or equal to start, FALSE will be returned.

$length The length of the intercepted string If length is not provided, the returned substring will start from the start position until the end of the string.

Example:

$str = 'a,b,c,d,';
$rest  = substr(  $str,0,strlen($str)-1);
echo $rest;
Copy after login

Output:

a,b,c
Copy after login
Related recommendations:

php training

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

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