使用 PHP 从 xml 文件中提取模式?
Extract pattern from xml file using PHP?
我有一个远程 XML 文件。我需要阅读,找到一些值并将它们保存在数组中。
我已经加载文件(没问题):
$xml_external_path = 'http://example.com/my-file.xml';
$xml = file_get_contents($xml_external_path);
在这个文件中有很多实例:
<unico>4241</unico>
<unico>234</unico>
<unico>534534</unico>
<unico>2345334</unico>
我只需要提取这些字符串的数量并将它们保存在一个数组中。我想我需要使用这样的模式:
$pattern = '/<unico>(.*?)<\/unico>/';
但我不确定下一步该做什么。请记住,它是一个 .xml 文件。
结果应该是一个像这样的填充数组:
$my_array = array (4241, 234, 534534,2345334);
你可以试试这个,它基本上只是循环遍历文件的每一行并找到 XML <unico>
标签之间的任何内容。
<?php
$file = "./your.xml";
$pattern = '/<unico>(.*?)<\/unico>/';
$allVars = array();
$currentFile = fopen($file, "r");
if ($currentFile) {
// Read through file
while (!feof($currentFile)) {
$m_sLine = fgets($currentFile);
// Check for sitename validity
if (preg_match($pattern, $m_sLine) == true) {
$curVar = explode("<unico>", $m_sLine);
$curVar = explode("</unico>", $curVar[1]);
$allVars[] = $curVar[0];
}
}
}
fclose($currentFile);
print_r($allVars);
这就是你想要的吗? :)
您可以更好地使用 XPath 来读取 XML 文件。 XPath 是 DOMDocument 的变体,专注于读取和编辑 XML 文件。您可以使用基于简单 Unix 路径语法的模式查询 XPath 变量。所以 //
表示任何地方, ./
表示相对于所选节点。 XPath->query()
将 return 一个 DOMNodelist 与所有节点根据模式。以下代码将执行您想要的操作:
$xmlFile = "
<unico>4241</unico>
<unico>234</unico>
<unico>534534</unico>
<unico>2345334</unico>";
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xmlFile);
$xpath = new DOMXPath($xmlDoc);
// This code returns a DOMNodeList of all nodes with the unico tags in the file.
$unicos = $xpath->query("//unico");
//This returns an integer of how many nodes were found that matched the pattern
echo $unicos->length;
您可以在此处找到有关 XPath 及其语法的更多信息:XPath on Wikipedia#syntax
DOMNodeList实现了Traversable,所以可以使用foreach()来遍历。如果你真的想要一个平面数组,你可以简单地转换是使用像 question #15807314:
中的简单代码
$unicosArr = array();
foreach($unicos as $node){
$unicosArr[] = $node->nodeValue;
}
使用preg_match_all:
<?php
$xml = '<unico>4241</unico>
<unico>234</unico>
<unico>534534</unico>
<unico>2345334</unico>';
$pattern = '/<unico>(.*?)<\/unico>/';
preg_match_all($pattern,$xml,$result);
print_r($result[0]);
我有一个远程 XML 文件。我需要阅读,找到一些值并将它们保存在数组中。
我已经加载文件(没问题):
$xml_external_path = 'http://example.com/my-file.xml';
$xml = file_get_contents($xml_external_path);
在这个文件中有很多实例:
<unico>4241</unico>
<unico>234</unico>
<unico>534534</unico>
<unico>2345334</unico>
我只需要提取这些字符串的数量并将它们保存在一个数组中。我想我需要使用这样的模式:
$pattern = '/<unico>(.*?)<\/unico>/';
但我不确定下一步该做什么。请记住,它是一个 .xml 文件。
结果应该是一个像这样的填充数组:
$my_array = array (4241, 234, 534534,2345334);
你可以试试这个,它基本上只是循环遍历文件的每一行并找到 XML <unico>
标签之间的任何内容。
<?php
$file = "./your.xml";
$pattern = '/<unico>(.*?)<\/unico>/';
$allVars = array();
$currentFile = fopen($file, "r");
if ($currentFile) {
// Read through file
while (!feof($currentFile)) {
$m_sLine = fgets($currentFile);
// Check for sitename validity
if (preg_match($pattern, $m_sLine) == true) {
$curVar = explode("<unico>", $m_sLine);
$curVar = explode("</unico>", $curVar[1]);
$allVars[] = $curVar[0];
}
}
}
fclose($currentFile);
print_r($allVars);
这就是你想要的吗? :)
您可以更好地使用 XPath 来读取 XML 文件。 XPath 是 DOMDocument 的变体,专注于读取和编辑 XML 文件。您可以使用基于简单 Unix 路径语法的模式查询 XPath 变量。所以 //
表示任何地方, ./
表示相对于所选节点。 XPath->query()
将 return 一个 DOMNodelist 与所有节点根据模式。以下代码将执行您想要的操作:
$xmlFile = "
<unico>4241</unico>
<unico>234</unico>
<unico>534534</unico>
<unico>2345334</unico>";
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xmlFile);
$xpath = new DOMXPath($xmlDoc);
// This code returns a DOMNodeList of all nodes with the unico tags in the file.
$unicos = $xpath->query("//unico");
//This returns an integer of how many nodes were found that matched the pattern
echo $unicos->length;
您可以在此处找到有关 XPath 及其语法的更多信息:XPath on Wikipedia#syntax
DOMNodeList实现了Traversable,所以可以使用foreach()来遍历。如果你真的想要一个平面数组,你可以简单地转换是使用像 question #15807314:
中的简单代码$unicosArr = array();
foreach($unicos as $node){
$unicosArr[] = $node->nodeValue;
}
使用preg_match_all:
<?php
$xml = '<unico>4241</unico>
<unico>234</unico>
<unico>534534</unico>
<unico>2345334</unico>';
$pattern = '/<unico>(.*?)<\/unico>/';
preg_match_all($pattern,$xml,$result);
print_r($result[0]);