如何使用 SimpleXMLElement search/filter xml 文件中的节点内容 - php
How to search/filter node content inside of xml file with SimpleXMLElement - php
我需要 filter/search 来自 XXML 文件的所有链接(png、jpg、mp3),但我卡在那里了。例如,我确实获取了所有 mp3,但我知道它在那里,但是例如,如果我将其他文件放在路径不同的地方,那么它就不会检测到它。
foreach($xml->BODY->GENERAL->SOUNDS->SOUND as $a){
echo '<a href="'.$a->PATH.'">'.$a->PATH.'</a><br>';
}
Example XML
您可以获得每个文件的扩展名并将其与 "accepted extensions" 的数组进行比较。然后用,continue
跳转写成link:
$accepted_exts = ['png','jpg','mp3'];
foreach($xml->BODY->GENERAL->SOUNDS->SOUND as $a) {
$path = $a->PATH;
$ext = strtolower(substr($path, strrpos($path, '.') + 1));
if (!in_array($ext, $accepted_exts)) continue ; // continue to next iteration
echo '<a href="'.$path.'">'.$path.'</a><br>'; // write the link
}
要获取其他 links:
$accepted_exts = ['png','jpg','mp3'];
$links = [] ;
foreach($xml->HEAD as $items) {
foreach ($items as $item) {
$path = (string)$item;
if (!in_array(get_ext($path), $accepted_exts)) continue ; // continue to next iteration
$links[] = $path ;
}
}
foreach($xml->BODY->GENERAL->SOUNDS->SOUND as $a) {
$path = $a->PATH;
if (!in_array(get_ext($path), $accepted_exts)) continue ; // continue to next iteration
$links[] = $path ;
}
foreach ($links as $path) {
echo '<a href="'.$path.'">'.$path.'</a><br>'; // write the link
}
function get_ext($path) {
return strtolower(substr($path, strrpos($path, '.') + 1));
}
将输出:
<a href="http://player.glifing.com/img/Player/blue.png">http://player.glifing.com/img/Player/blue.png</a><br>
<a href="http://player.glifing.com/img/Player/blue_intro.png">http://player.glifing.com/img/Player/blue_intro.png</a><br>
<a href="http://player.glifing.com/upload/fondoinstrucciones2.jpg">http://player.glifing.com/upload/fondoinstrucciones2.jpg</a><br>
<a href="http://player.glifing.com/upload/stopbet2.png">http://player.glifing.com/upload/stopbet2.png</a><br>
<a href="http://player.glifing.com/upload/goglif2.png">http://player.glifing.com/upload/goglif2.png</a><br>
<a href="http://player.glifing.com/img/Player/Glif 3 OK.png">http://player.glifing.com/img/Player/Glif 3 OK.png</a><br>
<a href="http://player.glifing.com/img/Player/BetPensant.png">http://player.glifing.com/img/Player/BetPensant.png</a><br>
<a href="http://player.glifing.com/audio/Player/si.mp3">http://player.glifing.com/audio/Player/si.mp3</a><br>
<a href="http://player.glifing.com/audio/Player/no.mp3">http://player.glifing.com/audio/Player/no.mp3</a><br>
为了不必知道哪些单独的标签可能包含 URL,您可以使用 XPath 搜索以 "http://" 或 "https://" 开头的任何文本内容。然后处理每个部分以检查扩展。
$xml = simplexml_load_file("data.xml");
$extensions = ['png', 'jpg', 'mp3'];
$links = $xml->xpath('//text()[starts-with(normalize-space(), "http://")
or starts-with(normalize-space(), "https://")]');
foreach ( $links as $link ) {
$link = trim(trim($link),"_");
$path = parse_url($link, PHP_URL_PATH);
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
if ( in_array($extension, $extensions)) {
// Do something
echo $link.PHP_EOL;
}
else {
echo "Rejected:".$link.PHP_EOL;
}
}
我发现使用 trim()
有助于清理 URL 后面有空行(或至少有一些额外的空格)。并将它们全部转换为较低的以便于检查。
你可能不需要被拒绝的位,但我把它放进去是为了测试我的代码。
你必须重复上面的内容
我需要 filter/search 来自 XXML 文件的所有链接(png、jpg、mp3),但我卡在那里了。例如,我确实获取了所有 mp3,但我知道它在那里,但是例如,如果我将其他文件放在路径不同的地方,那么它就不会检测到它。
foreach($xml->BODY->GENERAL->SOUNDS->SOUND as $a){
echo '<a href="'.$a->PATH.'">'.$a->PATH.'</a><br>';
}
Example XML
您可以获得每个文件的扩展名并将其与 "accepted extensions" 的数组进行比较。然后用,continue
跳转写成link:
$accepted_exts = ['png','jpg','mp3'];
foreach($xml->BODY->GENERAL->SOUNDS->SOUND as $a) {
$path = $a->PATH;
$ext = strtolower(substr($path, strrpos($path, '.') + 1));
if (!in_array($ext, $accepted_exts)) continue ; // continue to next iteration
echo '<a href="'.$path.'">'.$path.'</a><br>'; // write the link
}
要获取其他 links:
$accepted_exts = ['png','jpg','mp3'];
$links = [] ;
foreach($xml->HEAD as $items) {
foreach ($items as $item) {
$path = (string)$item;
if (!in_array(get_ext($path), $accepted_exts)) continue ; // continue to next iteration
$links[] = $path ;
}
}
foreach($xml->BODY->GENERAL->SOUNDS->SOUND as $a) {
$path = $a->PATH;
if (!in_array(get_ext($path), $accepted_exts)) continue ; // continue to next iteration
$links[] = $path ;
}
foreach ($links as $path) {
echo '<a href="'.$path.'">'.$path.'</a><br>'; // write the link
}
function get_ext($path) {
return strtolower(substr($path, strrpos($path, '.') + 1));
}
将输出:
<a href="http://player.glifing.com/img/Player/blue.png">http://player.glifing.com/img/Player/blue.png</a><br>
<a href="http://player.glifing.com/img/Player/blue_intro.png">http://player.glifing.com/img/Player/blue_intro.png</a><br>
<a href="http://player.glifing.com/upload/fondoinstrucciones2.jpg">http://player.glifing.com/upload/fondoinstrucciones2.jpg</a><br>
<a href="http://player.glifing.com/upload/stopbet2.png">http://player.glifing.com/upload/stopbet2.png</a><br>
<a href="http://player.glifing.com/upload/goglif2.png">http://player.glifing.com/upload/goglif2.png</a><br>
<a href="http://player.glifing.com/img/Player/Glif 3 OK.png">http://player.glifing.com/img/Player/Glif 3 OK.png</a><br>
<a href="http://player.glifing.com/img/Player/BetPensant.png">http://player.glifing.com/img/Player/BetPensant.png</a><br>
<a href="http://player.glifing.com/audio/Player/si.mp3">http://player.glifing.com/audio/Player/si.mp3</a><br>
<a href="http://player.glifing.com/audio/Player/no.mp3">http://player.glifing.com/audio/Player/no.mp3</a><br>
为了不必知道哪些单独的标签可能包含 URL,您可以使用 XPath 搜索以 "http://" 或 "https://" 开头的任何文本内容。然后处理每个部分以检查扩展。
$xml = simplexml_load_file("data.xml");
$extensions = ['png', 'jpg', 'mp3'];
$links = $xml->xpath('//text()[starts-with(normalize-space(), "http://")
or starts-with(normalize-space(), "https://")]');
foreach ( $links as $link ) {
$link = trim(trim($link),"_");
$path = parse_url($link, PHP_URL_PATH);
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
if ( in_array($extension, $extensions)) {
// Do something
echo $link.PHP_EOL;
}
else {
echo "Rejected:".$link.PHP_EOL;
}
}
我发现使用 trim()
有助于清理 URL 后面有空行(或至少有一些额外的空格)。并将它们全部转换为较低的以便于检查。
你可能不需要被拒绝的位,但我把它放进去是为了测试我的代码。
你必须重复上面的内容