我正在尝试从下面的字符串中提取与给定正则表达式匹配的子字符串:
“Lorem ipsum dolor sit amet。Lorem <xy:abc_ref d_id="1234">
ipsum dolor sit amet。Lorem ipsum dolor sit amet。<xy:abc_ref d_id="1234">
”
正则表达式确实符合预期。但是,由于某种原因,我只能访问第一个解析值。即使计数器 (count($matches)) 表明有两个结果,请参阅输出。
$value = 'Lorem ipsum dolor sit amet. <xy:abc_ref d_id="1234">Lorem ipsum dolor sit amet. <xy:abc_ref d_id="5678">Lorem ipsum dolor sit amet.<xy:abc_ref d_id="1234">';
来源:
function test($value)
{
$RegEx = '/<xy:abc_ref ([^>]{0,})>/';
$n = preg_match($RegEx,$value,$matches);
print("Results count: " . count($matches)."<br>");
print("matches[0]: " . $matches[0]."<br>");
print("matches[1]: " . $matches[1]."<br>");
print("matches[2]: " . $matches[2]."<br>");
}
echo test($value);
输出:
Results count: 2
matches[0]:
matches[1]: d_id="1234"
matches[2]:
谢谢, LG