Zabs
Zabs

Reputation: 14142

Using an expression to remove part of a string

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?

Upvotes: 0

Views: 110

Answers (6)

Gabber
Gabber

Reputation: 5452

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.

Upvotes: -1

Vipin Jain
Vipin Jain

Reputation: 1402

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);

Upvotes: 2

Manoj
Manoj

Reputation: 373

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
}

Upvotes: 2

Madara's Ghost
Madara's Ghost

Reputation: 174957

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"];

Upvotes: 2

Baba
Baba

Reputation: 95101

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)

Upvotes: 1

Laurence
Laurence

Reputation: 60038

$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"

Upvotes: 0

Related Questions