从php字符串中提取特定数据

时间:2016-11-16 02:45:33

标签: php

我有一个以以下格式存储在数据库中的网址

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并再次查询数据库,但这并不理想,因为我希望尽可能避免其他请求。

我真的不知道最好(正确)的方法。

1 个答案:

答案 0 :(得分:4)

Robbie 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
)
相关问题