I need to extract a portion of urls using php. The last 6 segments of the url are the part I need. The first part of the url varies in length and number of directories. So if
Use this, substituting $url as you wish:
$url= "https://www.random.vov/part1/part2/part3/2016/08/file.pdf";
preg_match("%/[^/]*?/[^/]*?/[^/]*?/[^/]*?/[^/]*?/[^/]*?$%", $url, $matches);
echo $matches[0];
best regards!
Your approach is fine, though adding parse_url
in there to isolate just the path will help a lot:
$path = parse_url($url, PHP_URL_PATH); // just the path part of the URL
$parts = explode('/', $path); // all the components
$parts = array_slice($parts, -6); // the last six
$path = implode('/', $parts); // back together as a string
Try it online at 3v4l.org.
Now, to qualify: if you only need the string part of the path, then use parse_url
. If, however, you need to work with each of the segments (such as removing only the last six, as asked), then use the common pattern of explode
/manipulate/implode
.
I have left each of these steps separate in the above so you can debug and choose the parts that work best for you.