我正在尝试读取php中链接中的一个数字。但我不知道该怎么做。
http://example.com/Productdetail.asp?SID=72&ProductID=8640
如何在不同时读取72
的情况下读取数字8640
。
ProductID=
<-
它将永远在那里。
SID=X
<-
这个有时会出现在那里,而在其他页面上则会被忽略。并且数字可以在1-999
、之间变化
有没有办法说ProductID= "read the next 4 numbers and save in string"
感谢
您可以使用explode()
获得此输出
$link='http://example.com/Productdetail.asp?SID=72&ProductID=8640';
$links=explode('ProductID=',$link);
echo $link[1]; //this is your number
简单使用
echo $_GET['ProductID'];//8640
您可以使用preg_match
作为
$str = "http://example.com/Productdetail.asp?SID=72&ProductID=8640";
preg_match('/&ProductID=('d+)?/',$str,$match);
echo $match[1];
或者您可以简单地使用parse_str
作为
$str = "http://example.com/Productdetail.asp?SID=72&ProductID=8640";
parse_str($str,$res);
echo $res['ProductID'];
您可以使用:
strpos
获取ProductID=的索引substr
从ProductID=获取字符串,直到字符串结束str_replace
替换ProductID=部件
<?php
$str = "http://example.com/Productdetail.asp?SID=72&ProductID=8640";
echo str_replace("ProductID=", "", substr($str, strpos($str, "ProductID=")));
?>