PHP Preg_match_all on XML/GML 多行输出
PHP Preg_match_all on XML/GML output on multiple lines
我尝试将多行 XML/GML 输出与 WFS 服务的 preg_match_all() 相匹配。我收到了一堆数据,这些数据在 public 服务器上可供所有人使用。我尝试使用 s 和 m 标志 ,但运气不佳。
我收到的数据如下所示:
<zwr:resultaat>
<zwr:objectBeginTijd>2012-09-18</zwr:objectBeginTijd>
<zwr:resultaatHistorie>
<zwr:datumInvoeren>2012-10-31</zwr:datumInvoeren>
<zwr:invoerder>
<zwr:voornaam>Joep</zwr:voornaam>
<zwr:achternaam>Koning, de</zwr:achternaam>
<zwr:email>jdekoning@hhdelfland.nl</zwr:email>
<zwr:telefoon>015-2608166</zwr:telefoon>
<zwr:organisatie>
<zwr:bedrijfsnaam>Hoogheemraadschap van Delfland</zwr:bedrijfsnaam>
<zwr:adres>
<zwr:huisnummer>32</zwr:huisnummer>
<zwr:postcode>2611AL</zwr:postcode>
<zwr:straat>Phoenixstraat</zwr:straat>
<zwr:woonplaats>DELFT</zwr:woonplaats>
</zwr:adres>
<zwr:email>info@hhdelfland.nl</zwr:email>
<zwr:telefoon>(015) 260 81 08</zwr:telefoon>
<zwr:website>http://www.hhdelfland.nl/</zwr:website>
</zwr:organisatie>
</zwr:invoerder>
</zwr:resultaatHistorie>
<zwr:risicoNiveau>false</zwr:risicoNiveau>
<zwr:numeriekeWaarde>0.02</zwr:numeriekeWaarde>
<zwr:eenheid>kubieke millimeter per liter</zwr:eenheid>
<zwr:hoedanigheid>niet van toepassing</zwr:hoedanigheid>
<zwr:kwaliteitsOordeel>Normale waarde</zwr:kwaliteitsOordeel>
<zwr:parameterGrootheid>
<zwr:grootheid>Biovolume per volume eenheid</zwr:grootheid>
<zwr:object>Microcystis</zwr:object>
</zwr:parameterGrootheid>
<zwr:analyseProces>
<zwr:analyserendeInstantie>AQUON</zwr:analyserendeInstantie>
</zwr:analyseProces>
</zwr:resultaat>
全是荷兰语,但这与问题的上下文无关。案例是我想搜索这段代码的多行并获取标签之间的值。我也尝试单独读取所有内容(结果很好),但是因为有多种标签组合(有时会使用或不使用标签),这会混淆我收到的数据并且获取的数据中没有结构数据。
我认为读取一整套标签是个好主意,这样我就可以将数据放在一起。当前 preg_match_all() 我的代码是:
preg_match_all("/<zwr:risicoNiveau>(.*)<\/zwr:risicoNiveau><zwr:numeriekeWaarde>(.*)<\/zwr:numeriekeWaarde><zwr:eenheid>(.*)<\/zwr:eenheid><zwr:hoedanigheid>(.*)<\/zwr:hoedanigheid>
<zwr:kwaliteitsOordeel>(.*)<\/zwr:kwaliteitsOordeel><zwr:parameterGrootheid><zwr:object>(.*)<\/zwr:object><zwr:grootheid>(.*)<\/zwr:grootheid><\/zwr:parameterGrootheid>/m", $content, $stof);
正如您所看到的,我想从一个 preg_match_all() 中读取多个值,这将给我一个包含多个数组的数组。
如何依次读取多个标签(它们在不同的行上?)?当我使用 var_dump() 显示所有数据时,它向我显示了一个没有数据的多维数组。 s 和 m 标志 对我不起作用?难道我做错了什么?欢迎使用 PHP 中的其他方法!
1.) 您需要在标签之间添加whitespace \s
。
<\/zwr:risicoNiveau>
\s*
<zwr:numeriekeWaarde>
...
2.) 在捕获组中进一步使用 .*?
来匹配非 greedy.
<zwr:risicoNiveau>(.*?)<\/zwr:risicoNiveau>
3.) 使用 x
flag(自由间距模式)提高正则表达式的可读性。
Regex demo at regex101
注意:使用exclusion ([^<]*?)
rather than (.*?)
for forcing the format like this. To match the remaining tags, use optional quantifier ?
on optional tags like this和可选的<zwr:object>
$pattern = '~
<zwr:risicoNiveau>(.*?)</zwr:risicoNiveau>\s*
<zwr:numeriekeWaarde>(.*?)</zwr:numeriekeWaarde>\s*
<zwr:eenheid>(.*?)</zwr:eenheid>\s*
<zwr:hoedanigheid>(.*?)</zwr:hoedanigheid>\s*
<zwr:kwaliteitsOordeel>(.*?)</zwr:kwaliteitsOordeel>\s*
<zwr:parameterGrootheid>\s*
<zwr:grootheid>(.*?)</zwr:grootheid>\s*
<zwr:object>(.*?)</zwr:object>\s*
</zwr:parameterGrootheid>
~sx';
PREG_SET_ORDER Orders results so that $matches[0]
is an array of first set of matches, $matches[1]
is an array of second set of matches, and so on... read more in the PHP MANUAL
if(preg_match_all($pattern, $str, $out, PREG_SET_ORDER) > 0)
print_r($out);
我尝试将多行 XML/GML 输出与 WFS 服务的 preg_match_all() 相匹配。我收到了一堆数据,这些数据在 public 服务器上可供所有人使用。我尝试使用 s 和 m 标志 ,但运气不佳。 我收到的数据如下所示:
<zwr:resultaat>
<zwr:objectBeginTijd>2012-09-18</zwr:objectBeginTijd>
<zwr:resultaatHistorie>
<zwr:datumInvoeren>2012-10-31</zwr:datumInvoeren>
<zwr:invoerder>
<zwr:voornaam>Joep</zwr:voornaam>
<zwr:achternaam>Koning, de</zwr:achternaam>
<zwr:email>jdekoning@hhdelfland.nl</zwr:email>
<zwr:telefoon>015-2608166</zwr:telefoon>
<zwr:organisatie>
<zwr:bedrijfsnaam>Hoogheemraadschap van Delfland</zwr:bedrijfsnaam>
<zwr:adres>
<zwr:huisnummer>32</zwr:huisnummer>
<zwr:postcode>2611AL</zwr:postcode>
<zwr:straat>Phoenixstraat</zwr:straat>
<zwr:woonplaats>DELFT</zwr:woonplaats>
</zwr:adres>
<zwr:email>info@hhdelfland.nl</zwr:email>
<zwr:telefoon>(015) 260 81 08</zwr:telefoon>
<zwr:website>http://www.hhdelfland.nl/</zwr:website>
</zwr:organisatie>
</zwr:invoerder>
</zwr:resultaatHistorie>
<zwr:risicoNiveau>false</zwr:risicoNiveau>
<zwr:numeriekeWaarde>0.02</zwr:numeriekeWaarde>
<zwr:eenheid>kubieke millimeter per liter</zwr:eenheid>
<zwr:hoedanigheid>niet van toepassing</zwr:hoedanigheid>
<zwr:kwaliteitsOordeel>Normale waarde</zwr:kwaliteitsOordeel>
<zwr:parameterGrootheid>
<zwr:grootheid>Biovolume per volume eenheid</zwr:grootheid>
<zwr:object>Microcystis</zwr:object>
</zwr:parameterGrootheid>
<zwr:analyseProces>
<zwr:analyserendeInstantie>AQUON</zwr:analyserendeInstantie>
</zwr:analyseProces>
</zwr:resultaat>
全是荷兰语,但这与问题的上下文无关。案例是我想搜索这段代码的多行并获取标签之间的值。我也尝试单独读取所有内容(结果很好),但是因为有多种标签组合(有时会使用或不使用标签),这会混淆我收到的数据并且获取的数据中没有结构数据。
我认为读取一整套标签是个好主意,这样我就可以将数据放在一起。当前 preg_match_all() 我的代码是:
preg_match_all("/<zwr:risicoNiveau>(.*)<\/zwr:risicoNiveau><zwr:numeriekeWaarde>(.*)<\/zwr:numeriekeWaarde><zwr:eenheid>(.*)<\/zwr:eenheid><zwr:hoedanigheid>(.*)<\/zwr:hoedanigheid>
<zwr:kwaliteitsOordeel>(.*)<\/zwr:kwaliteitsOordeel><zwr:parameterGrootheid><zwr:object>(.*)<\/zwr:object><zwr:grootheid>(.*)<\/zwr:grootheid><\/zwr:parameterGrootheid>/m", $content, $stof);
正如您所看到的,我想从一个 preg_match_all() 中读取多个值,这将给我一个包含多个数组的数组。
如何依次读取多个标签(它们在不同的行上?)?当我使用 var_dump() 显示所有数据时,它向我显示了一个没有数据的多维数组。 s 和 m 标志 对我不起作用?难道我做错了什么?欢迎使用 PHP 中的其他方法!
1.) 您需要在标签之间添加whitespace \s
。
<\/zwr:risicoNiveau>
\s*
<zwr:numeriekeWaarde>
...
2.) 在捕获组中进一步使用 .*?
来匹配非 greedy.
<zwr:risicoNiveau>(.*?)<\/zwr:risicoNiveau>
3.) 使用 x
flag(自由间距模式)提高正则表达式的可读性。
Regex demo at regex101
注意:使用exclusion ([^<]*?)
rather than (.*?)
for forcing the format like this. To match the remaining tags, use optional quantifier ?
on optional tags like this和可选的<zwr:object>
$pattern = '~
<zwr:risicoNiveau>(.*?)</zwr:risicoNiveau>\s*
<zwr:numeriekeWaarde>(.*?)</zwr:numeriekeWaarde>\s*
<zwr:eenheid>(.*?)</zwr:eenheid>\s*
<zwr:hoedanigheid>(.*?)</zwr:hoedanigheid>\s*
<zwr:kwaliteitsOordeel>(.*?)</zwr:kwaliteitsOordeel>\s*
<zwr:parameterGrootheid>\s*
<zwr:grootheid>(.*?)</zwr:grootheid>\s*
<zwr:object>(.*?)</zwr:object>\s*
</zwr:parameterGrootheid>
~sx';
PREG_SET_ORDER Orders results so that
$matches[0]
is an array of first set of matches,$matches[1]
is an array of second set of matches, and so on... read more in the PHP MANUAL
if(preg_match_all($pattern, $str, $out, PREG_SET_ORDER) > 0)
print_r($out);