为了简单起见,我们不会使用环顾四周,虽然我说我们需要s
修饰符,但我错了,它仅用于将新行与.
我们不会在此处使用的点匹配,因此\s
匹配新行:
$string = <<<JSO
swatch: 'http://abc.com/aa.jpg',
zoom:[
'http://abc.com/bb.jpg'
],
large:[
'http://abc.com/cc.jpg'
],
JSO;
preg_match_all('/zoom:\[\s*\'(?<img>[^\']*)\'\s*\]/', $string, $m);
print_r($m['img']);
输出:
Array
(
[0] => http://abc.com/bb.jpg
)
解释:
/ # Starting delimiter
zoom:\[ # Matches zoom:[
\s* # Matches spaces, newlines, tabs 0 or more times
\' # Matches '
(?<img>[^\']*) # named group, matches everything until ' found
\' # Matches '
\s* # Matches spaces, newlines, tabs 0 or more times
\] # Matches ]
/ # Ending delimiter