【问题标题】:How to search/filter node content inside of xml file with SimpleXMLElement - php如何使用 SimpleXMLElement - php 在 xml 文件中搜索/过滤节点内容
【发布时间】:2018-08-29 00:57:54
【问题描述】:

我需要从 XXML 文件中过滤/搜索所有链接(png、jpg、mp3),但我被困在那里。例如,我这样做是为了获取所有 mp3,但我知道它在那里,但是例如,如果我将其他文件放在路径不同的地方,那么它就不会检测到它。

    foreach($xml->BODY->GENERAL->SOUNDS->SOUND as $a){
    echo '<a href="'.$a->PATH.'">'.$a->PATH.'</a><br>';
    }

Example XML

【问题讨论】:

  • 您要查看$a-&gt;PATH的扩展名吗?
  • 是的,对所有 xml 文件做一个 foreach 来检查扩展名,然后显示这个链接

标签: php xml simplexml


【解决方案1】:

您可以获取每个文件的扩展名并将其与“接受的扩展名”数组进行比较。然后用continue跳过写链接:

$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
}

获取其他链接:

$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>

【讨论】:

  • 谢谢!一件事,你知道如何迭代整个 xml 来检查它吗?在“示例 XML”中是它的图像。
  • @ToniTJK 您能否在您的答案中添加一个 XML 示例?谢谢。
  • yeees,它对我帮助很大,再次感谢! (PD:抱歉重复,以防万一,我无法编辑)
  • 不客气。还可以看看@NigelRen 的有趣答案。 (别忘了删除你的answer,以及你之前的评论;)谢谢!)
【解决方案2】:

为了不必知道哪些单独的标签可能包含 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(或至少一些额外的空格)。并将它们全部转换为较低的以使检查更容易。

你可能不需要被拒绝的位,但我把它放进去测试我的代码。

你必须重复上面的内容

【讨论】:

    猜你喜欢
    • 2013-07-13
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 2015-11-11
    • 2013-11-05
    • 2017-05-15
    • 1970-01-01
    相关资源
    最近更新 更多