1

I have a string in PHP that is a URI with all arguments:

$string = http://domain.com/php/doc.php?arg1=0&arg2=1&arg3=0

I want to completely remove an argument and return the remain string. For example I want to remove arg3 and end up with:

$string = http://domain.com/php/doc.php?arg1=0&arg2=1

I will always want to remove the same argument (arg3), and it may or not be the last argument.

Thoughts?

EDIT: there might be a bunch of wierd characters in arg3 so my prefered way to do this (in essence) would be:

$newstring = remove $_GET["arg3"] from $string;

4 Answers 4

8

There's no real reason to use regexes here, you can use string and array functions instead.

You can explode the part after the ? (which you can get using substr to get a substring and strrpos to get the position of the last ?) into an array, and use unset to remove arg3, and then join to put the string back together.:

$string = "http://domain.com/php/doc.php?arg1=0&arg2=1&arg3=0";
$pos = strrpos($string, "?"); // get the position of the last ? in the string
$query_string_parts = array();

foreach (explode("&", substr($string, $pos + 1)) as $q)
{
  list($key, $val) = explode("=", $q);
  if ($key != "arg3")
  {
    // keep track of the parts that don't have arg3 as the key
    $query_string_parts[] = "$key=$val";
  }
}

// rebuild the string
$result = substr($string, 0, $pos + 1) . join($query_string_parts);

See it in action at http://www.ideone.com/PrO0a

0
0
preg_replace("arg3=[^&]*(&|$)", "", $string)

I'm assuming the url itself won't contain arg3= here, which in a sane world should be a safe assumption.

0
$new = preg_replace('/&arg3=[^&]*/', '', $string);
0

This should also work, taking into account, for example, page anchors (#) and at least some of those "weird characters" you mention but don't seem worried about:

function remove_query_part($url, $term)
{
    $query_str = parse_url($url, PHP_URL_QUERY);
    if ($frag = parse_url($url, PHP_URL_FRAGMENT)) {
        $frag = '#' . $frag;
    }
    parse_str($query_str, $query_arr);
    unset($query_arr[$term]);
    $new = '?' . http_build_query($query_arr) . $frag;
    return str_replace(strstr($url, '?'), $new, $url);
}

Demo:

$string[] = 'http://domain.com/php/doc.php?arg1=0&arg2=1&arg3=0';
$string[] = 'http://domain.com/php/doc.php?arg1=0&arg2=1';
$string[] = 'http://domain.com/php/doc.php?arg1=0&arg2=1&arg3=0#frag';
$string[] = 'http://domain.com/php/doc.php?arg1=0&arg2=1&arg3=0&arg4=4';
$string[] = 'http://domain.com/php/doc.php';
$string[] = 'http://domain.com/php/doc.php#frag';
$string[] = 'http://example.com?arg1=question?mark&arg2=equal=sign&arg3=hello';

foreach ($string as $str) {
    echo remove_query_part($str, 'arg3') . "\n";
}

Output:

http://domain.com/php/doc.php?arg1=0&arg2=1
http://domain.com/php/doc.php?arg1=0&arg2=1
http://domain.com/php/doc.php?arg1=0&arg2=1#frag
http://domain.com/php/doc.php?arg1=0&arg2=1&arg4=4
http://domain.com/php/doc.php
http://domain.com/php/doc.php#frag
http://example.com?arg1=question%3Fmark&arg2=equal%3Dsign

Tested only as shown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.