-1

我有这个$value= 24153; 我有一个字段可以保存一个或多个像这样{"id":"2","value":["3"]}或这样的值,{"id":"2","value":["3","4"]}或者{"id":"2","value":["3","4","2"]} 我有这个可以正常工作的正则表达式,但如果 ONE 值存在则返回。如果字段中有多个值,我需要改进这个正则表达式。

REGEXP BINARY \'(.*{"id":"2","value":\["[^\"]*['.$value.'][^\"]*",?)+\]}.*\'
4

2 回答 2

0

这是一个正则表达式,可以满足您的要求-但我必须同意您的常识-存储 json 然后使用正则表达式从中提取数据令人作呕

REGEXP BINARY \'(.*{"id":"2","value":\[("[^\"]*",)*"'.$value.'"(,"[^\"]*")*)+\]}.*\'
于 2013-07-02T15:04:48.760 回答
-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)
}
于 2013-06-20T13:52:06.317 回答