0

我是正则表达式的新手。

我有:

$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

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

感谢!

4

1 回答 1

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']; 

打印

29-31 48-46,47
于 2013-05-02T16:30:10.387 回答