0

I want to remove "&start=2" from a given URL. This is what I tried:

$uri = "http://test.com/test/?q=Marketing&start=2";

$newuri = str_replace("&start=","",$url);

echo $newuri;
1
  • Please can you put your question a bit more clearly? Commented Oct 15, 2009 at 9:06

6 Answers 6

6

You'll want to use preg_replace instead for this:

$newuri = preg_replace('/&start=(\d+)/','',$uri);
Sign up to request clarification or add additional context in comments.

3 Comments

It's unnecessary to capture the \d+ in parentheses
Right... but it helps from a readability standpoint. Not critical in this example, but it's good practice for when gigantic regex patterns come around.
As the asker is a beginner, perhaps a little more elaboration as to what the code is doing?
3

You are passing $url as an argument to str_replace. But the variable that has the url is called $uri.

$uri = "http://test.com/test/?q=Marketing&start=2";

$newuri = str_replace("&start=2","",$uri);

...

3 Comments

This won't actually remove the numeric value 2 of start as the OP requires. A generic solution allowing for arbitrary numbers would need a regular expression replacement function.
Fixed. Now it removes the value 2 without using regex.
What if they have 3 instead of 2?
1

Just to throw a regex-free solution out there:

// Grab the individual components of the URL
$uri_components = parse_url($uri); 

// Take the query string from the url, break out the key:value pairs
// then put the keys and values into an associative array
foreach (explode('&', $uri_components['query']) as $pair) {
    list($key,$value) = explode('=', $pair);
    $query_params[$key] = $value;
}

// Remove the 'start' pair from the array and start reassembling the query string
unset($query_params['start']);
foreach ($query_params as $key=>$value)
    $value ? $new_query_params[] = $key."=".$value : $new_query_params[] = $key;

// Now reassemble the whole URL (including the bits removed by parse_url)
$uri_components['scheme'] .= "://";
$uri_components['query'] = "?".implode($new_query_params,"&");
$newuri = implode($uri_components);

Admittedly it's massively verbose compared to the regex-based solutions, but it might provide some extra flexibility down the line?

Comments

0

Remember that it is still a valid URI if the position of the querystring elements is changed. So the start parameter may be the first, hence it may be preceded by a ? instead of a &.

So this regex covers both cases:

preg_replace("#[\?&]start=\d+#", '', $uri)

Comments

0

Try This regex free solution:

$uri = "http://test.com/test/?q=Marketing&start=2";
$QueryPos = strpos($uri, '&start='); //find the position of '&start='
$newURI = substr($uri, 0, -(strlen($uri)-$QueryPos)); //remove everything from the start of $QueryPos to end
echo $newURI; //OUTPUT: http://test.com/test/?q=Marketing

Comments

0
if(isset($_GET['start']))

{

   $pageNo = $_GET['start'];

   $TmpString = '&start='.$pageNo;

  $newuri = str_replace($TmpString,"",$url);

  // removed "&start=2"

  echo $newuri;

} 

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.