在 PHPglob
中:XML 文件、xpath
所有节点、preg_match_all
它们的文本以及如果匹配,获取节点的 xpathgetNodePath()
并输出:
$pattern = '/home|house|guide/iu';
foreach (glob('data/*.xml') as $file)
{
foreach (simplexml_load_file($file)->xpath('//*') as $node)
{
if (!preg_match_all($pattern, $node, $matches)) continue;
printf(
"\"%s\" in %s, xpath: %s\n", implode('", "', $matches[0]),
basename($file), dom_import_simplexml($node)->getNodePath()
);
}
}
结果(示例):
"Guide" in iana-charsets-2013-03-05.xml, xpath: /*/*[7]/*[158]/*[4]
"Guide" in iana-charsets-2013-03-05.xml, xpath: /*/*[7]/*[224]/*[2]
"Guide" in iana-charsets-2013-03-05.xml, xpath: /*/*[7]/*[224]/*[4]
"guide" in rdf-dmoz.xml, xpath: /*/*[4]/d:Description
"guide" in rdf-dmoz.xml, xpath: /*/*[5]/d:Description
好问题顺便说一句。