extract part of a path with filename with php

后端 未结 2 1085
[愿得一人]
[愿得一人] 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!

提交回复
热议问题