我已经使用 php 打开并列出文件夹中的文件,但我想以某种方式将其转换为 if else 语句,以便如果它来自“深色”背景文件夹,则会显示一个白色徽标,如果它来自一个“浅色”背景文件夹将显示一个黑色徽标。
<?php
//path to directory to open
$directory = "backgrounds/dark";
$dir_handle = @opendir($directory) or die("Unable to open folder");
while(false !== ($file = readdir($dir_handle)))
{
if($file != '.' && $file != '..')
{
echo "{image : 'http://occa-art.com/backgrounds/dark/".$file."'}, ";
}
}
closedir($dir_handle);
?>
使用 {image: 'http://' } 格式是因为图像在超大背景脚本中列出。
- -编辑 - -
也许像这样(未经测试)来确定背景图像来自哪个文件夹:
<?php
$doc = new DOMDocument();
$doc->loadHTML($htmlAsString);
$xpath = new DOMXPath($doc);
$nodeList = $xpath->query('//img[@class="bg"]/@src');
for ($i = 0; $i < $nodeList->length; $i++) {
# Xpath query for attributes gives a NodeList containing DOMAttr objects.
# http://php.net/manual/en/class.domattr.php
echo $nodeList->item($i)->value . "<br/>\n";
}
$bg_url = $nodeList->item($i)->value; // not sure about this?
$parent = dirname("$bg_url");
if ($parent == 'dark') { ?>
<img src="<?php bloginfo('template_url'); ?>/images/OCCAIndexHeaderwhite.png" alt="OCCA logo" />
<?php } else { ?>
<img src="<?php bloginfo('template_url'); ?>/images/OCCAIndexHeaderblack.png" alt="OCCA logo" />
<?php } ?>