2016-11-16 76 views
2

我有存储在数据库中的以下格式的URL提取特定的数据

index.php?main_page=product_info&cPath=1_11&products_id=568

我希望能够提取CPATH数据,1_11在这种情况下,产品ID“ 568'分为两个单独的变量。请注意,cPath值可以从单个数字(如23)变为一系列数字和下划线(如17_25_31)。如果提取cPath太困难,我可以在提取后再使用products_id并再次查询数据库,但这并不理想,因为我想尽可能避免附加请求。

我真的不知道最好(正确)的方式去做这件事。

回答

4

由罗比Averill的建议更精确的方法

//first lets the the query string alone 
$string=parse_url('index.php?main_page=product_info&cPath=1_11&products_id=568', PHP_URL_QUERY); 

parse_str($string,$moo); 

print_r($moo); 

输出:

Array 
(
    [main_page] => product_info 
    [cPath] => 1_11 
    [products_id] => 568 
) 

我原来的建议。

parse_str('index.php?main_page=product_info&cPath=1_11&products_id=568',$moo); 

print_r($moo); 

输出:

Array 
(
    [index_php?main_page] => product_info 
    [cPath] => 1_11 
    [products_id] => 568 
) 
+1

更好的将可能是'parse_url'第一然后就'查询字符串 –

+1

我想过parse_str',但他只想'cPath'和'products_id'所以我认为让它变得很简单。但我会补充说,答案;-) – 2016-11-16 02:59:35

+0

工程很漂亮,谢谢。很容易将值赋给变量$ cPath =($ moo ['cPath']); $ products_id =($ moo ['products_id']); –