2013-05-02 48 views
0

我是正则表达式的新手。PHP preg_match如何从字符串开始“charcter =”并以“&”结尾

我有:

$str = 'manufacturer=1,2,3&brand=5,4&filter=29-31+48-46,47&price=150-700'; 
$find = 'filter'; // for example. it can also be a price or brand or something 

if (strpos($str, $find) !== FALSE) 
{ 
$matches = array(); 
preg_match('/' . $find . '=???/', $str, $matches); 
var_dump($matches); 
} 

???代表,何正则表达式我必须使用的preg_match像单独使用情况以及我如何解析这个字符串数据:

[0] = 29-31+48-46,47 // data parsed from "filter=" to "&" character 

我不能使用爆炸或类似的命令,因为参数字符串中的位置不固定。

谢谢!

回答

3

不要使用正则表达式,它是工作的错误工具。使用parse_str()

$str = 'manufacturer=1,2,3&brand=5,4&filter=29-31+48-46,47&price=150-700'; 
parse_str($str, $array); 
echo $array['filter']; 

prints:从未使用过此功能

29-31 48-46,47 
+0

。但是这很漂亮。现在想要找到它的用途。 – EnigmaRM 2013-05-02 16:35:47

+0

太棒了!谢谢。输出是29-31 48-46,47(加号缺失),但这没有问题。 – netoper 2013-05-02 16:39:21

+0

@glirpi - 这是因为查询字符串中的“+”意味着空间。 – nickb 2013-05-02 16:42:00

相关问题