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

问题描述 投票:0回答:1

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

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

谢谢

php regex regular-language
1个回答
1
投票

您可以使用parse_url提取URL的路径部分,然后使用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

© www.soinside.com 2019 - 2024. All rights reserved.