Crop/remove HTTP query from end of a URL?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Crop/remove HTTP query from end of a URL?

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post 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));
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post 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.
Post Reply