简体   繁体   中英

extract part of a path with filename with php

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 I have a url like this:

https://www.random.ccc/random2/part1/part2/part3/2017/08/file.txt

or this:

https://www.random.vov/part1/part2/part3/2016/08/file.pdf

What I need is this:

/part1/part2/part3/2017/08/file.txt

or this:

/part1/part2/part3/2016/08/file.pdf

I have tried this:

$string = implode("/",array_slice(explode("/",$string,8),6,4));

which works ok on the first example but not the second. I am not so good with regex and I suppose that is the way. What is the most graceful solution?

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM