Home  >  Article  >  Backend Development  >  How to remove trailing characters in php

How to remove trailing characters in php

藏色散人
藏色散人Original
2021-02-28 16:59:122070browse

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);

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)

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:

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!

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