How to extract some specific string from url?

时间:2019-04-17 01:57:38

标签: php regex regular-language

Using PHP function or Php regex, how can I extract only 568429042356.html or 568429042356 from the link below?

https://sub.example.com/offer/568429042356.html?spm=b2611038.mof001.21.10393eccdCb5XA&sk=consign

Thank you

1 个答案:

答案 0 :(得分:1)

You can use parse_url to extract the path part of the URL, and then basename to get the filename from that:

$url = 'https://sub.example.com/offer/568429042356.html?spm=b2611038.mof001.21.10393eccdCb5XA&sk=consign';
echo basename(parse_url($url, PHP_URL_PATH));

Output:

568429042356.html

Demo on 3v4l.org