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 OverflowI 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);
$str = preg_replace('/\?\//', '?', $str);
Edit: See CMS' answer. It's late, I should know better.
Efficient way to remove multiple irrelevant characters from a string?
How to remove numbers from end of the string
How to remove the last character of a string in PHP?
$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.
$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.
What if I want to remove the last character only when it matches a pattern?
Can I remove only slashes or commas from the end?
There are several methods available, and they can sometimes be made to perform exactly the same task, like preg_replace/str_replace. But, perhaps you want to remove brackets only from the beginning or end of the string; in which case preg_replace works. But, if there could be several brackets, preg_replace can do the job too. But trim is easier and makes more sense.
preg_replace() - removes beginning and trailing brackets
$widget_id = preg_replace(array('/^\[/','/\]$/'), '',$widget_text);
str_replace() - this removes brackets anywhere in the text
$widget_id = str_replace(array('[',']'), '',$widget_text);
trim() - trims brackets from beginning and end
$widget_id = trim($widget_text,'[]')
substr() - does the same as trim() (assuming the widget text does not include any closing brackets within the text)
$widget_id = substr($widget_text,
$start = strspn($widget_text, '['),
strcspn($widget_text, ']') - $start
);
$widget_id = str_replace('[', '', str_replace(']', '', $widget_text));
So I have this string--call it "String_X" and it has a lot of text but I want to replace things like whitespace and punctuation.
I wrote a function which takes String_X and applies trim() to it, followed by several rounds of str_replace().
Is there a more efficient way to remove multiple characters from the string without having to use a str_replace() for every possible character I don't want remaining in the string?