1
0
Fork 0

Pick longest match always

pull/1192/head
Jordi Boggiano 2012-10-08 18:35:22 +02:00
parent a4e5f000d5
commit dc033e33d6
1 changed files with 9 additions and 4 deletions

View File

@ -164,16 +164,21 @@ class JsonManipulator
}
if (preg_match('{"'.preg_quote($name).'"\s*:}i', $children)) {
if (preg_match('{"'.preg_quote($name).'"\s*:\s*(?:[0-9.]+|null|true|false|"[^"]+"|\{'.self::$RECURSE_BLOCKS.'\})}', $children, $matches)) {
$children = preg_replace('{,\s*'.preg_quote($matches[0]).'}i', '', $children, -1, $count);
if (preg_match_all('{"'.preg_quote($name).'"\s*:\s*(?:[0-9.]+|null|true|false|"[^"]+"|\{'.self::$RECURSE_BLOCKS.'\})}', $children, $matches)) {
$bestMatch = '';
foreach ($matches[0] as $match) {
if (strlen($bestMatch) < strlen($match)) {
$bestMatch = $match;
}
}
$children = preg_replace('{,\s*'.preg_quote($bestMatch).'}i', '', $children, -1, $count);
if (1 !== $count) {
$children = preg_replace('{'.preg_quote($matches[0]).'\s*,?\s*}i', '', $children, -1, $count);
$children = preg_replace('{'.preg_quote($bestMatch).'\s*,?\s*}i', '', $children, -1, $count);
if (1 !== $count) {
return false;
}
}
}
}
if (!trim($children)) {