举个例子,我想用 PHP 从一个给定的 URL 链接中提取查询参数 goods_id 的值。
实现方法
测试用的 PHP 版本:PHP 7.1.20
$product_link = "https://mobile.yangkeduo.com/goods1.html?goods_id=7717242955&page_from=23&share_uin=QXBIHIEFDSK7RH4TRIYMP4BW7E_GEXDA&refer_share_id=30a0099ec95f469e931800d00606d03e&refer_share_uid=4035794210&refer_share_channel=message";
$query = parse_url($product_link, PHP_URL_QUERY);
parse_str($query, $items);
$goods_id = $items['goods_id']; // 7717242955
parse_url
php artisan tinker
Psy Shell v0.9.8 (PHP 7.1.20-1+ubuntu18.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> $url = "https://mobile.yangkeduo.com/goods1.html?goods_id=7717242955&page_from=23&share_uin=QXBIHIEFDSK7RH4TRIYMP4BW7E_GEXDA&refer_share_id=30a0099ec95f469e931800d00606d03e&refer_share_uid=4035794210&refer_share_channel=message";
=> "https://mobile.yangkeduo.com/goods1.html?goods_id=7717242955&page_from=23&share_uin=QXBIHIEFDSK7RH4TRIYMP4BW7E_GEXDA&refer_share_id=30a0099ec95f469e931800d00606d03e&refer_share_uid=4035794210&refer_share_channel=message"
>>> parse_url($url);
=> [
"scheme" => "https",
"host" => "mobile.yangkeduo.com",
"path" => "/goods1.html",
"query" => "goods_id=7717242955&page_from=23&share_uin=QXBIHIEFDSK7RH4TRIYMP4BW7E_GEXDA&refer_share_id=30a0099ec95f469e931800d00606d03e&refer_share_uid=4035794210&refer_share_channel=message",
]
>>> parse_url($url, PHP_URL_QUERY);
=> "goods_id=7717242955&page_from=23&share_uin=QXBIHIEFDSK7RH4TRIYMP4BW7E_GEXDA&refer_share_id=30a0099ec95f469e931800d00606d03e&refer_share_uid=4035794210&refer_share_channel=message"
parse_str
>>> $query = parse_url($url, PHP_URL_QUERY);
=> "goods_id=7717242955&page_from=23&share_uin=QXBIHIEFDSK7RH4TRIYMP4BW7E_GEXDA&refer_share_id=30a0099ec95f469e931800d00606d03e&refer_share_uid=4035794210&refer_share_channel=message"
>>> parse_str($query, $items);
=> null
>>> $items
=> [
"goods_id" => "7717242955",
"page_from" => "23",
"share_uin" => "QXBIHIEFDSK7RH4TRIYMP4BW7E_GEXDA",
"refer_share_id" => "30a0099ec95f469e931800d00606d03e",
"refer_share_uid" => "4035794210",
"refer_share_channel" => "message",
]
>>>
微信关注我哦 👍
我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式