我有$value= 24153;
我有一个字段,可以包含一个或多个值,例如{"id":"2","value":["3"]}
,或者像{"id":"2","value":["3","4"]}
或{"id":"2","value":["3","4","2"]}
我有这个正则表达式工作正常,但如果ONE值存在则返回。
我需要改进这个正则表达式,以便在字段中有多个值。
REGEXP BINARY \'(.*{"id":"2","value":\["[^\"]*['.$value.'][^\"]*",?)+\]}.*\'
答案 0 :(得分:0)
这里有一个可以做你想要的正则表达式 - 但我必须同意你的常识 - 存储json然后使用正则表达式从中提取数据令人作呕
REGEXP BINARY \'(.*{"id":"2","value":\[("[^\"]*",)*"'.$value.'"(,"[^\"]*")*)+\]}.*\'
答案 1 :(得分:-1)
$value = 4;
$subject_a = '{"id":"2","value":["4"]}';
$subject_b = '{"id":"2","value":["1","4","1","1"]}';
$subject_c = '{"id":"2","value":["1","1","1","1","1","44","1","1"]}';
$pattern = '/{"id":"2","value":\[("|\d|,)*"' . $value . '"("|\d|,)*\]/';
$matches[0] = preg_match($pattern,$subject_a);
$matches[1] = preg_match($pattern,$subject_b);
$matches[2] = preg_match($pattern,$subject_c);
echo '<pre>';
var_dump($matches);
echo '</pre>';
结果将是:
array(3) {
[0]=> int(1)
[1]=> int(1)
[2]=> int(0)
}