0

This is probably simple however I am not the best with expressions.. I am trying to get the following string from..

http://www.yoursite.com/offers/838?&SITEID=2172

to this.. using an expression that will remove the ?&SITEID and the dynamic id which will vary

http://www.yoursite.com/offers/838  

Can anyone suggest the best/simplest method to do this?

2
  • Do you mean anything past ? or just siteid? Commented Oct 12, 2012 at 10:08
  • Just want to remove anything past &SITEID - so any other querystring parts remain Commented Oct 12, 2012 at 10:20

6 Answers 6

2

Check this function:

$str = 'http://www.yoursite.com/offers/838?&SITEID=2172';

function remove_query_arg($var, $url = NULL){
    if(!$url){
        $url = $_SERVER['REQUEST_URI'];
    }
    $parsed_url = parse_url($url);

    $query_vars = explode('&', $parsed_url['query']);


    foreach($query_vars as $key => $value){
        $query_vars[$key] = explode('=', $query_vars[$key]);
        $query_variables[$query_vars[$key][0]] = $query_vars[$key][1];
    }

    if(is_array($var)){
        foreach($var as $value){
            unset($query_variables[$value]);
        }
    }
    elseif(is_string($var)){
        unset($query_variables[$var]);
    }

    $query_vars = array();

    foreach($query_variables as $key => $value){
        $query_vars[] = $key.($value !== NULL || !empty($value) ? '='.$value : '');
    }
    $query_str = '';
    $query_str = implode('&',$query_vars);

    return (isset($parsed_url['scheme']) && !empty($parsed_url['scheme']) ? $parsed_url['scheme'].'://' : '').$parsed_url['host'].(isset($parsed_url['path']) && !empty($parsed_url['path']) ? $parsed_url['path'] : '').(!empty($query_str) ? '?'.$query_str : '');
}

echo remove_query_arg('SITEID', $str);
Sign up to request clarification or add additional context in comments.

Comments

2

This is a URL, so parse it as one, with parse_url().

$url = "http://www.yoursite.com/offers/838?&SITEID=2172";
$parts = parse_url($url);

$url = $parts["scheme"] . "://" . $parts["host"] . $parts["path"];

Comments

2

Using explode function returns an array

$url=http://www.yoursite.com/offers/838?&SITEID=2172
$result=explode('?',$url)
print_r($result);

output

array
{
 [0]=>http://www.yoursite.com/offers/838
 [1]=>?&SITEID=2172
}

Comments

1

A valid URL only has one ? so you can just use explode to break it into 2 parts

$url = "http://www.yoursite.com/offers/838?&SITEID=2172";
list($path, $query) = explode("?", $url, "2");
var_dump($path);

Output

string 'http://www.yoursite.com/offers/838' (length=34)

Comments

0
$url = "http://www.yoursite.com/offers/838?&SITEID=2172"; 
$str = substr($url, strpos($url, 0, "?&SITEID"));
// $str results in "http://www.yoursite.com/offers/838"

Comments

-1

If you want to keep the part before the ? you can search

^(.+?)(\?&SITEID|$)

and replace with

$1

You search non greedily from the beginning of the line ^ to the first ?&SITEID and leave out the rest. If no ?&SITEID is found you get the entire line by arriving at the end of the string with $

| is the OR operator that tells the regex "Stop at the first ?&SITEID or at the end of the string"

EDIT:

After the comment where you explain your need to keep the rest of the querystring I suggest you a different approach: find

&?SITEID=[^&\s]+

being

  • &? an optional & at the beginning of the string
  • SITEID= the string you are looking for followed by
  • [^&\s]+ any number of non&, nonspace character

and remove it from the string. However, being this the case, I'd go with a non-regex, url-specific approach like suggested in the other answers.

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.