Home > Backend Development > PHP Tutorial > How to Remove Specific Characters from the End of a String in PHP?

How to Remove Specific Characters from the End of a String in PHP?

Patricia Arquette
Release: 2024-11-08 18:33:01
Original
468 people have browsed it

How to Remove Specific Characters from the End of a String in PHP?

Removing Specific Characters at String End in PHP

One common task in string manipulation is removing specific characters from the end of a string. For instance, you may need to delete a period if it's the final character.

Let's consider an example:

$string = "something here.";
$output = "something here";
Copy after login

Solution Using rtrim

To remove a specific character from the end of a string, you can use the rtrim function. This function removes any trailing characters specified in its second argument from the input string.

In the given example, to remove a period from the end of the string, you can use:

$output = rtrim($string, '.');
Copy after login

This operation would assign the trimmed string to the $output variable, resulting in:

$output = 'something here';
Copy after login

Reference

  • [rtrim](https://www.php.net/manual/en/function.rtrim.php)

The above is the detailed content of How to Remove Specific Characters from the End of a String in PHP?. For more information, please follow other related articles on the PHP Chinese website!

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 Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template