Reputation: 343
Title is pretty much self explanatory but just to be more clear...
I want that all those 5 option :
http://www.example.com/directory1/directory2/?q=this/that/other
http://www.example.com/directory1/directory2/file.html
http://www.example.com/directory1/directory2/file.html?q=sometext
http://www.example.com/directory1/directory2/
http://www.example.com/directory1/directory2/?q=sometext
will return:
http://www.example.com/directory1/directory2/
So in other words...
Find the LAST ? (if exists) and THEN find the LAST /
and ignore anything afterwards (no need to capture everything afterwards)
preg_match_all('/<a(.*?)href={HOW DO I SAY FIND THE **LAST ** /FROM THAT LINK, KEEP IT AND IGNORE EVERYTHING AFTERWARDS }>/s',$content,$images);
Upvotes: 1
Views: 217
Reputation: 778
Try to use parse_url
which returns host, path and query separate.
// you can write something like
$url = parse_url($url);
$path = explode('/',$url['path']);
$lastDir = end($path);
Upvotes: 0
Reputation: 78994
Regex is great but use the proper tool for the job:
$parts = parse_url($url);
$result = $parts['scheme'].$parts['host'].$parts['path'];
You can use dirname()
on the path if needed. Also notice there are other components that you may want such as port, etc...
Upvotes: 0
Reputation: 1908
Use
preg_match_all('~[^\?]*/~', $str, $match);
to get everything without parts following ?
.
Upvotes: 1
Reputation: 81
You can do something like that :
$uri = "http://www.example.com/directory1/directory2/file.html";
$pos = strrpos($uri, "/");
$shortUri = substr(uri, 0, $pos);
Upvotes: 1