![]() |
VOOZH | about |
There is a lot of options to remove all specific characters at the end of a string. Some of them are discussed below:
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:
Example: This example uses rtrim() function to remove ‘.’ from the end.
GeeksforGeeks is a best platform
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:
Example 2: This example uses preg_replace() function to remove ‘.’ from the end.
GeeksforGeeks is a best platform
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:
Hello, World
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:
Array ( [0] => Hello World [1] => PHP is great [2] => Let's code )
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:
Original String: Hello, world. Modified String: Hello, world