[php] PHP removing a character in a string

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!

This question is related to php string

The answer is


$splitPos = strpos($url, "?/");
if ($splitPos !== false) {
    $url = substr($url, 0, $splitPos) . "?" . substr($url, $splitPos + 2);
}

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

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


$splitPos = strpos($url, "?/");
if ($splitPos !== false) {
    $url = substr($url, 0, $splitPos) . "?" . substr($url, $splitPos + 2);
}

While a regexp would suit here just fine, I'll present you with an alternative method. It might be a tad faster than the equivalent regexp, but life's all about choices (...or something).

$length = strlen($urlString);
for ($i=0; $i<$length; i++) {
  if ($urlString[$i] === '?') {
    $urlString[$i+1] = '';
    break;
  }
}

Weird, I know.


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

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


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

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


While a regexp would suit here just fine, I'll present you with an alternative method. It might be a tad faster than the equivalent regexp, but life's all about choices (...or something).

$length = strlen($urlString);
for ($i=0; $i<$length; i++) {
  if ($urlString[$i] === '?') {
    $urlString[$i+1] = '';
    break;
  }
}

Weird, I know.


$splitPos = strpos($url, "?/");
if ($splitPos !== false) {
    $url = substr($url, 0, $splitPos) . "?" . substr($url, $splitPos + 2);
}

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

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


While a regexp would suit here just fine, I'll present you with an alternative method. It might be a tad faster than the equivalent regexp, but life's all about choices (...or something).

$length = strlen($urlString);
for ($i=0; $i<$length; i++) {
  if ($urlString[$i] === '?') {
    $urlString[$i+1] = '';
    break;
  }
}

Weird, I know.