Home > Backend Development > PHP Problem > How to remove trailing characters in php

How to remove trailing characters in php

藏色散人
Release: 2023-03-08 10:52:02
Original
2146 people have browsed it

php method to remove tail characters: 1. Remove the tail characters through the "substr($arr_str,0,strlen($arr_str)-1);" statement; 2. Use "substr($arr_str, 0, -1)" statement to remove the trailing characters; 3. Delete the specified characters at the end of the string through rtrim, the syntax is "rtrim($arr_str, "specified character")".

How to remove trailing characters in php

The operating environment of this tutorial: Windows 7 system, PHP 8 version, Dell G3 computer.

Summary of the method of deleting the last character in PHP:

Method 1:

substr($arr_str,0,strlen($arr_str)-1);
Copy after login

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

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, ",")
Copy after login

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 is simply prepared for this need!

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!

[Recommended learning: "PHP Video Tutorial"]

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

Related labels:
php
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template