0

Want to remove p2variable from url string, below are 3 cases if case 3 also remove ? sign.

case 1: http://www.domain.com/myscript.php?p1=xyz&p2=10&p3=ghj
result: http://www.domain.com/myscript.php?p1=xyz&p3=ghj

case 2: http://www.domain.com/myscript.php?p2=10&p3=ghj
result: http://www.domain.com/myscript.php?p3=ghj

case 3: http://www.domain.com/myscript.php?p2=10
result: http://www.domain.com/myscript.php

Want to achieve result with single preg_replace expression.

3
  • Did you see my answer on you 1st question about this? stackoverflow.com/questions/14432731/… Commented Jan 21, 2013 at 6:39
  • 1
    I've incorporated your idea into my answer, no need to put answers inside the question itself :) Commented Jan 21, 2013 at 7:03
  • Thanks Sean, that was a different approach from my end. Commented Jan 21, 2013 at 7:14

3 Answers 3

1

Don't use regular expressions when dealing with URL values. It's much easier (and safer) to handle them as a URL instead of plain text.

This could be one way to do it:

  1. Split the url first and parse the query string
  2. Take the parameter out
  3. Rebuild the url

The below code is an example of such an algorithm:

// remove $qs_key from query string of $url
// return modified url value
function clean_url_qs($url, $qs_key)
{
    // first split the url in two parts (at most)
    $parts = explode('?', $url, 2);

    // check whether query string is passed        
    if (isset($parts[1])) {
      // parse the query string into $params
      parse_str($parts[1], $params);

      // unset if $params contains $qs_key
      if (array_key_exists($qs_key, $params)) {
          // remove key
          unset($params[$qs_key]);
          // rebuild the url
          return $parts[0] . 
              (count($params) ? '?' . http_build_query($params) : '');
      }
    }
    // no change required
    return $url;
}

Test code:

echo clean_url('http://www.domain.com/myscript.php?p1=xyz&p2=10&p3=ghj', 'p2'), "\n";
echo clean_url('http://www.domain.com/myscript.php?p2=10&p3=ghj', 'p2'), "\n";
echo clean_url('http://www.domain.com/myscript.php?p2=10', 'p2'), "\n";
Sign up to request clarification or add additional context in comments.

Comments

0

Found this in one of my old projects (a bit of shitcode, but...), may help you:

$unwanted_param = 'p2';
$s = 'http://www.domain.com/myscript.php?p1=xyz&p2=10&p3=ghj';
$s = parse_url($s);
$params = explode('&', $s['query']);
$out_params = array();
foreach ($params as $key => &$param) {
    list($name, $value) = explode('=', $param);
    if ($unwanted_param == $name) {
        unset($params[$key]);
    } else {
        $out_params[$name] = $value;
    }
}

$query = '?' . http_build_query($out_params);

$result = $s['scheme'] . '://' . $s['host'] . $s['path'] . $query;

var_dump($result);

2 Comments

Hint: use parse_str instead of exploding the query manually.
@DCoder, thanks! That's an old part of code - I was young and stupid :)
0

Using preg_replace, something like

$url = preg_replace('!([\?&]p2=[^&\?$]+)!i', '', $url);

However, personally I'd do the following

if (strpos($url, '?') !== false) {
    list($domain, $qstring) = explode('?', $url, 2);
    parse_str($qstring, $params);
    if (isset($params['p2'])) {
        unset($params['p2']);
    }
    $qstring = !empty($params) ? '?' . http_build_query($params) : '';
    $url = $domain . $qstring;
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.