Open In App

Removing occurrences of a specific character from end of a string in PHP

Last Updated : 18 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

There is a lot of options to remove all specific characters at the end of a string. Some of them are discussed below:

Using rtrim() function

This function is an inbuilt function in PHP that removes whitespaces or other characters (if specified) from the right side of the string.

Syntax:

rtrim( $string, $charlist )

Parameters:

This function accepts two parameters as mentioned above and described below:

  • $string: It is mandatory parameter which specifies the string to be checked.
  • $charlist: It is an optional parameter. It specifies which characters to be removed from string.

Example: This example uses rtrim() function to remove ‘.’ from the end.

<?php

// Declare a variable and initialize it
$string = "GeeksforGeeks is a best platform.....";

// Use rtrim() function to trim
// string from right
echo rtrim($string, ".");

?>

Output
GeeksforGeeks is a best platform

Using preg_replace() function

This function is an inbuilt function in PHP which is used to perform a regular expression for search and replace the content.

Syntax:

preg_replace( $patt, $replace, $string, $limit, $count )

Parameters:

This function accepts five parameters as mentioned above and described below:

  • $patt: It contains string element which is used to search content and it can be a string or array of string.
  • $replace: It is mandatory parameter which specifies the string or an array with strings to replace.
  • $string: The string or an array with strings to search and replace.
  • $limit: Parameter specifies the maximum possible replacements for each pattern.
  • $count: Optional parameter. This variable will be filled with the number of replacements done.

Example 2: This example uses preg_replace() function to remove ‘.’ from the end.

<?php

// Declare a variable and initialize it
$string = "GeeksforGeeks is a best platform.....";

// Character which need to replace
$regex = "/\.+$/";

// Use preg_replace() function to replace 
// the character
echo preg_replace($regex, "", $string);
 
?>

Output
GeeksforGeeks is a best platform

Using substr() with strlen()

Using substr() with strlen(), iterate through the string and remove characters from the end based on the length of the string until the specified character is no longer at the end.

Example:

<?php
$string = "Hello, World!!!!";
$charToRemove = '!';
while (substr($string, -1) == $charToRemove) {
    $string = substr($string, 0, -1);
}
echo $string;
?>

Output
Hello, World

Using rtrim() with array_walk() Function

This method involves using array_walk() to apply rtrim() on each element of an array of strings, thereby removing specific characters from the end of each string.

Example:

<?php
$strings = ["Hello World.", "PHP is great.", "Let's code."];
$charlist = ".";

array_walk($strings, function(&$str) use ($charlist) {
    $str = rtrim($str, $charlist);
});

print_r($strings);
?>

Output
Array
(
    [0] => Hello World
    [1] => PHP is great
    [2] => Let's code
)

Using Regular Expressions with preg_match() and preg_replace()

Another approach to remove specific characters from the end of a string in PHP is by using regular expressions with preg_match() and preg_replace(). This method provides a flexible way to match and remove specific patterns at the end of a string.

Example:

<?php
$inputString = "Hello, world.";

// Use preg_replace to remove the period from the end of the string
$outputString = preg_replace('/\.$/', '', $inputString);

echo "Original String: " . $inputString . "\n";
echo "Modified String: " . $outputString;
?>

Output
Original String: Hello, world.
Modified String: Hello, world


Next Article

Similar Reads

three90RightbarBannerImg