您可以获取每个文件的扩展名并将其与“接受的扩展名”数组进行比较。然后用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>