Jump to content

Properly remove a query string


Dareros

Recommended Posts

Hi, i have a query string id=5&start=0 that i want to remove from the url and return the url without it. I am searching a cleaner way for doing it. Here is an example :

Before : http://www.example.com/?a=3&id=5&start=0
After : http://www.example.com/?a=3

Before : http://www.example.com/?id=5&start=0
After : http://www.example.com/

Before : http://www.example.com/?alpha=1&id=5&start=0&beta=2
After : http://www.example.com/?alpha=1&beta=2

Thank you

Edited by Dareros
Link to comment
Share on other sites

Use parse_url to get the pieces of the URL, parse_str to get the different values in the query string, unset() the ones you don't want, and put the URL back together using http_build_query to get the new query string.

 

[edit] Unless the query string can contain multiple values for the same key, like key=value1&key=value2&key=value3.

Edited by requinix
Link to comment
Share on other sites

The problem is that i have a big string with too many urls like the one their, so i need to do this task for each url i found on the string.

$url = 'http://www.example.com/?alpha=1&id=5&start=0&beta=2';
$query = 'id=5&start=0';
$string = "blabla ... blaba .. $url ..blabla ... $url .. blabla ....blablabla .. $url ... $url ... blabla ...";
/*
Algorithm :
1. Search any url that have the $query on it, 
2. remove the query and 
3. replace it by the new url in the same string.
So if $nurl is the new url without the query string in red, the final string should become:
$string = "blabla ... blaba .. $nurl ..blabla ... $nurl .. blabla ....blablabla .. $nurl ... $nurl ... blabla ...";
*/

Any help here is appreciated :tease-03:

Thank you.

Edited by Dareros
Link to comment
Share on other sites

Regular expressions. Search around, there's a lot around about what expression to use.

 

Then preg_replace_callback, which lets you put a function in to create the replacement string. In that function you do the "remove the query" work, eventually returning what you want the replacement string to be (be that the original and unmodified URL or an edited one).

Link to comment
Share on other sites

Will all of the urls start with the protocol or is it possible to have urls like www.somesite.com?

 

I understand what you mean, you want to regex starting by http or www to guess that this is a url. I have a better alternative because some times, it is a relative url and some times it is absolute with or without protocol. So the alternative i have is to remove all possible cases one by one. The possible cases we can have are :

1. '?id=5&start=0'
2. '?id=5&start=0&something'
3. 'something&id=5&start=0'
4. 'something1&id=5&start=0&something2'

Now, the replacements should be for each case :

1. ''
2. '?something'
3. 'something'
4. 'something1&something2'
Edited by Dareros
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.