如何从 url 中提取一些特定的字符串?

How to extract some specific string from url?

使用 PHP 函数或 Php 正则表达式,如何从下面的 link 中仅提取 568429042356.html 或 568429042356?

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

谢谢

您可以使用 parse_url to extract the path part of the URL, and then basename 从中获取文件名:

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

输出:

568429042356.html

Demo on 3v4l.org