PHP removing a character in a string


Question

My php is weak and I'm trying to change this string:

http://www.example.com/backend.php?/c=crud&m=index&t=care
                                   ^

to be:

http://www.example.com/backend.php?c=crud&m=index&t=care
                                  ^

removing the / after the backend.php?. Any ideas on the best way to do this?

Thanks!

1
36
2/3/2017 1:38:08 PM

Accepted Answer

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);
113
11/5/2008 6:49:01 AM

$str = preg_replace('/\?\//', '?', $str);

Edit: See CMS' answer. It's late, I should know better.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon