不要使用正则表达式来解析 xml/html...
话虽如此,问题是因为匹配结构如下所示:
array(
0 => array('whole match1', 'whole match 2', 'whole match 3'),
1 => array('subpattern match 1', 'subpattern match 2', 'subpattern match 3'),
);
因此,不要执行 foreach,而是执行以下操作:
if (!empty($matches)) {
foreach ($matches[1] as $value) {
echo $value;
}
}
或使用PREG_SET_ORDER
标记为 preg_match_all,这将产生如下数组结构:
array(
0 => array('whole match1', 'subpattern match 1'),
0 => array('whole match2', 'subpattern match 2'),
0 => array('whole match3', 'subpattern match 3'),
);
那么你的 foreach 将变成:
if (!empty($matches)) {
foreach ($matches as $match) {
echo $match[1];
}
}