I think that it's better to use simply str_replace, like the manual says:

If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of ereg_replace() or preg_replace().

<?
$badUrl = "http://www.site.com/backend.php?/c=crud&m=index&t=care";
$goodUrl = str_replace('?/', '?', $badUrl);
Answer from Christian C. Salvadó on Stack Overflow
🌐
PHP
php.net › manual › en › function.str-replace.php
PHP: str_replace - Manual
If you want to remove all dashes but one from the string '-aaa----b-c-----d--e---f' resulting in '-aaa-b-c-d-e-f', you cannot use str_replace. Instead, use preg_replace: <?php $challenge = '-aaa----b-c-----d--e---f'; echo str_replace('--', '-', $challenge).'<br>'; echo preg_replace('/--+/', '-', $challenge).'<br>'; ?> This outputs the following: -aaa--b-c---d-e--f -aaa-b-c-d-e-f ... To remove all characters from string $b that exist in string $a: $a="ABC"; $b="teAsBtC"; echo str_replace(str_split($a),'',$b); Output: test To remove all characters from string $b that don't exist in string $a: $a="ABC"; $b="teAsBtC"; echo str_replace(str_split(str_replace(str_split($a),'',$b)),'',$b); Output: ABC
Discussions

Efficient way to remove multiple irrelevant characters from a string?
You can use regex then preg_replace, example: function remove_irrelevant_chars($string) { $pattern = '/[\s\p{P}]+/u'; return preg_replace($pattern, '', $string); } More on reddit.com
🌐 r/PHPhelp
11
4
March 25, 2023
How to remove numbers from end of the string
howdy jishnutp, this seems to do the job ... PM(.*)\d{1,}$ it finds PM and any final digits, then grabs anything between them. it works with your data set on regex101.com if you enable "multiline". take care, lee More on reddit.com
🌐 r/regex
7
3
June 26, 2017
People also ask

How to remove the last character of a string in PHP?
1. Use substr()
$text = "Hello!";
$result = substr($text, 0, -1);
echo $result; // Hello

substr() cuts part of a string. The 0 means start at the first character. The -1 tells PHP to stop before the last character.

2. Use rtrim()
$text = "Hello!";
$result = rtrim($text, "!");
echo $result; // Hello

rtrim() removes chosen characters from the end. Here it removes the !.

3. Use mb_substr() (for UTF-8)
$text = "مرحبا!";
$result = mb_substr($text, 0, -1, "UTF-8");
echo $result; // مرحبا

mb_substr() is safe for Unicode text. It works like substr() but supports multi-byte characters such as Arabic.

🌐
flatcoding.com
flatcoding.com › home › how to remove the last character from a string in php
How to Remove the Last Character from a String in PHP - FlatCoding
What if I want to remove the last character only when it matches a pattern?
Use preg_replace() with a regular expression like /character$/ to match it safely.
🌐
flatcoding.com
flatcoding.com › home › how to remove the last character from a string in php
How to Remove the Last Character from a String in PHP - FlatCoding
Can I remove only slashes or commas from the end?
Yes. Use rtrim($string, "/,") to remove slashes or commas from the end only.
🌐
flatcoding.com
flatcoding.com › home › how to remove the last character from a string in php
How to Remove the Last Character from a String in PHP - FlatCoding
🌐
W3Schools
w3schools.com › php › func_string_str_replace.asp
PHP str_replace() Function
Replace the characters "world" in the string "Hello world!" with "Peter".
🌐
Flexiple
flexiple.com › php › php-trim
PHP trim() function: Remove characters from string - Flexiple
September 3, 2022 - The trim() function in PHP removes whitespace or any other predefined character from both the left and right sides of a string.
🌐
Alvin Alexander
alvinalexander.com › php › php-string-strip-characters-whitespace-numbers
PHP: How to strip unwanted characters from a string | alvinalexander.com
February 3, 2024 - Finally, if you want to strip all characters from your string other than letters, numbers, and whitespace, this regular expression will do the trick: $res = preg_replace("/[^a-zA-Z0-9\s]/", "", $string); (Again, I leave that output as "an exercise for the reader.") Before I go, I thought it ...
🌐
FlatCoding
flatcoding.com › home › how to remove the last character from a string in php
How to Remove the Last Character from a String in PHP - FlatCoding
August 23, 2025 - You can remove the last character from a PHP string using four methods: substr_replace, substr, mb_substr, and rtrim. Here’s an example..
Find elsewhere
🌐
W3Schools
w3schools.com › php › func_string_trim.asp
PHP trim() Function
❮ PHP String Reference · Remove ... Try it Yourself » · Share Link Copied · The trim() function removes whitespace and other predefined characters from both sides of a string....
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-remove-the-first-character-of-string-in-php
How to remove the first character of string in PHP? - GeeksforGeeks
July 11, 2025 - ... <?php $str = "geeks"; // Or we can write ltrim($str, $str[0]); $str = ltrim($str, 'g'); echo $str; ?> ... Here we can use it by two parameters one is the string and the other is the index. substr() return string from the second parameter ...
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-remove-special-character-from-string-in-php
How to Remove Special Character from String in PHP? - GeeksforGeeks
September 10, 2024 - The str_replace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (" ").
🌐
Delft Stack
delftstack.com › home › howto › php › remove special characters from string php
How to Remove Special Character in PHP | Delft Stack
February 2, 2024 - This is also a very useful built-in function, which is used to replace the special character from the string. str_replace() is also helps to replace that character with the removed character. This function contains few parameters, as introduced below. $search_str: It contains such value which we want to search in the given string. $replace_str: It stores a value that you want to replace, or you can also leave it empty if you only want the removal of a special character. ... See the example code. <?php $mainstr = "This is a sim'ple text;"; echo "Text before remove: \n" .
🌐
W3Docs
w3docs.com › php
How to Remove the Last Character from a String in PHP | W3Docs
The substr function extracts a substring. To remove the last character, specify a negative length: ... <‌?php $string = "Hello World!"; echo "Given string: " . $string . "\n"; echo "Updated string: " . substr($string, 0, -1) .
🌐
Hellonitish
hellonitish.com › blog › remove-first-or-last-character-from-a-string-in-php
Hello Nitish • How Can I Remove the First or Last Character from a String in PHP?
November 4, 2025 - The PHP substr() function returns the part of string specified by the start and length parameters. If you want to remove characters from the end of string, you can set the value of start to 0 and the value of length to a negative number.
🌐
Medium
medium.com › @ntohtambe › removing-special-characters-in-php-a41ca6d964d1
Removing Special Characters in PHP | by Tambe Salome | Medium
May 2, 2022 - When reading this text from the database, the escape characters are not removed automatically but there are some PHP functions that can help do that easily. This function strips off all backslashes from the string passed and returns a string.
🌐
PHP Tutorial
phptutorial.net › home › php tutorial › php rtrim
PHP rtrim: Remove Characters from the End of a String
April 7, 2025 - To remove the whitespace characters or other characters from the end of a string, you use the PHP rtrim() function.
🌐
GeeksforGeeks
geeksforgeeks.org › php › removing-occurrences-of-a-specific-character-from-end-of-a-string-in-php
Removing occurrences of a specific character from end of a string in PHP - GeeksforGeeks
July 18, 2024 - 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 = "/\.+$/"; // ...
🌐
Softwareinfosys
softwareinfosys.com › home › php › remove a certain character from a string in php
Remove a certain character from a string in PHP
The preg_replace() function performs a regular expression search and replace, and returns a resulting string. <?php $phone = '123-456-7890'; $phone = preg_replace('/-/', '', $phone); echo $phone; ?> Output: 1234567890 ... To remove the occurrences of multiple characters in a string using this method, we include them in our regular expression.