Page 1 of 1

Crop/remove HTTP query from end of a URL?

Posted: Thu Jun 26, 2008 8:11 pm
by JAB Creations
Does any one have a simple method of cropping off / removing HTTP queries from a URL?

In example I may have...
example.php?my_query

...and I'd want to change it to...
example.php

Re: Crop/remove HTTP query from end of a URL?

Posted: Thu Jun 26, 2008 8:19 pm
by John Cartwright
If you want just the filename without the query string,

Code: Select all

$basename = array_shift(explode('?', basename($url)));
If you want to simply remove the query string, and keep the rest of your url, i.e. http://foo.com/page.php?foo, then simply

Code: Select all

$basename = array_shift(explode('?', $url));

Re: Crop/remove HTTP query from end of a URL?

Posted: Thu Jun 26, 2008 8:22 pm
by JAB Creations
Yes, I was thinking that after I posted. That was pretty quick! :mrgreen: Is this technically regex?

What about this...?

Code: Select all

echo strtok('example.php?my_query','?');
I can't use string positioning because the url length will always vary though I know that if at all possible I should avoid regex.

Re: Crop/remove HTTP query from end of a URL?

Posted: Thu Jun 26, 2008 8:30 pm
by John Cartwright
JAB Creations wrote:Yes, I was thinking that after I posted. That was pretty quick! :mrgreen: Is this technically regex?

What about this...?

Code: Select all

echo strtok('example.php?my_query','?');
I can't use string positioning because the url length will always vary though I know that if at all possible I should avoid regex.
No it is not regex. Simple string manipulation.

strtok() essentially the same as explode(), and I also find it more readable.