Home  >  Article  >  Backend Development  >  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
Patricia ArquetteOriginal
2024-11-08 18:33:01373browse

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

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, '.');

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

$output = 'something here';

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!

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