PHP preg_match如何从字符串开始"字符="并以“&"”结尾


PHP preg_match how to get from string begin "charcter=" and end with "&"

我是正则表达式新手。

:

$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

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

谢谢!

不要使用正则表达式,这是错误的工具。使用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