在 PHP 中:
<?php
$input = '
class : putmeincoach,
id : random_id,
responsive : { [ type= text ; html = "< iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps/myplaces?ctz=480&ie=UTF8&ll=34.009394,-118.488514&spn=0.020687,0.020921&t=m&z=16"> < /iframe >" ; ] },
testsinglequotes: \'hello world\',
testdoublequotes: "hello hello"';
$pattern = '/
\b(\w+)\s*: # capture the word before the colon
\s*( # start capture group match after the colon
{[^}]*}\s*| # match everything between braces OR
"[^"]*"\s*| # match everything between double quotes OR
\'[^\']*\'\s*| # match everything between single OR
[^,]* # match everything up to the comma
) # end capture group
(?:,|$) # match comma or end of string (non-capture group)
/x';
preg_match_all($pattern ,$input, $matches);
print_r($matches);
?>
输出 PHP 5.3.13:
Array
(
[0] => Array
(
[0] => class : putmeincoach,
[1] => id : random_id,
[2] => responsive : { [ type= text ; html = "< iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps/myplaces?ctz=480&ie=UTF8&ll=34.009394,-118.488514&spn=0.020687,0.020921&t=m&z=16"> < /iframe >" ; ] },
[3] => testsinglequotes: 'hello world',
[4] => testdoublequotes: "hello hello"
)
[1] => Array
(
[0] => class
[1] => id
[2] => responsive
[3] => testsinglequotes
[4] => testdoublequotes
)
[2] => Array
(
[0] => putmeincoach
[1] => random_id
[2] => { [ type= text ; html = "< iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps/myplaces?ctz=480&ie=UTF8&ll=34.009394,-118.488514&spn=0.020687,0.020921&t=m&z=16"> < /iframe >" ; ] }
[3] => 'hello world'
[4] => "hello hello"
)
)
正则表达式是你的朋友