extract part of a path with filename with php

后端 未结 2 1084
[愿得一人]
[愿得一人] 2021-01-28 18:55

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

相关标签:
2条回答
  • 2021-01-28 19:04

    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!

    0 讨论(0)
  • 2021-01-28 19:18

    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.

    0 讨论(0)
提交回复
热议问题