Dareros Posted August 11, 2014 Share Posted August 11, 2014 (edited) 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 August 11, 2014 by Dareros Quote Link to comment Share on other sites More sharing options...
requinix Posted August 11, 2014 Share Posted August 11, 2014 (edited) 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 August 11, 2014 by requinix Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted August 11, 2014 Share Posted August 11, 2014 parse_url() http_build_query() Quote Link to comment Share on other sites More sharing options...
Dareros Posted August 11, 2014 Author Share Posted August 11, 2014 (edited) 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 Thank you. Edited August 11, 2014 by Dareros Quote Link to comment Share on other sites More sharing options...
requinix Posted August 11, 2014 Share Posted August 11, 2014 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). Quote Link to comment Share on other sites More sharing options...
Dareros Posted August 11, 2014 Author Share Posted August 11, 2014 The callback is easy, i am searching how to do the first task. Quote Link to comment Share on other sites More sharing options...
CroNiX Posted August 11, 2014 Share Posted August 11, 2014 Will all of the urls start with the protocol or is it possible to have urls like www.somesite.com? Quote Link to comment Share on other sites More sharing options...
Dareros Posted August 11, 2014 Author Share Posted August 11, 2014 (edited) 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 August 11, 2014 by Dareros Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.