我用php打开并列出文件夹中的文件,但是我想以某种方式把它变成if else语句,这样如果它来自'dark'背景文件夹,就会显示一个白色的标识,如果它来自“浅色”背景文件夹,则会显示黑色徽标。
<?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://'}格式,因为图像列在超级背景脚本中。
--- --- EDIT
可能是这样的(未经测试)来确定背景图像来自哪个文件夹:
<?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 } ?>