用于解析目录的PHP脚本,列出所有图像并添加class =" last"到目录中的最后一个图像 [英] PHP script to parse directory, list all images and add class="last" to the last image in the directory

查看:113
本文介绍了用于解析目录的PHP脚本,列出所有图像并添加class =" last"到目录中的最后一个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够解析目录并使用以下任何功能列出所有图像。我只需要在循环中最后一个元素的img标记中插入一个class =last属性。

I'm able to parse through the directory and list all images with any of the functions below. I just need to insert a class="last" attribute into the img tag of the last element in the loop.

此外,哪些函数最适合我我想做什么?

Also, which of these functions works best for what I'm trying to do?

任何帮助都非常感谢!

function get_images1() {

$exts = 'jpg jpeg png gif';

$str = ''; $i = -1; // Initialize some variables
$folder = './wp-content/uploads';

$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
    foreach($exts as $ext) { // for each extension check the extension
        if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
            //$str .= $file;
            $str .="<img src='wp-content/uploads/". $file ."' alt='" . $file . "' />";
            //if ($str) $str .= '|';
            ++$i;
        }
    }
}
echo $str;
closedir($handle); // Were not using it anymore
return $str;

}

function get_images2() {

//Open images directory
$dir = @ opendir("wp-content/uploads/");

//List files in uploads directory
while (($file = readdir($dir)) !== false)
{
if(ereg("(.*)\.(jpg|bmp|jpeg|png|gif)", $file))
    {
    echo '<img src="wp-content/uploads/'. $file .'" alt="" />';
    }
}
closedir($dir);
}

function get_images3() {

$dir = 'wp-content/uploads/';
$files = scandir($dir);
//print_r($files);
$num = count($files);
for($n=0; $n<$num; $n++) 
{
if(ereg("(.*)\.(jpg|bmp|jpeg|png|gif)", $files[$n]))
    {
    echo '<img src="wp-content/uploads/'. $files[$n] .'" alt="" />';
    }
}
}

function get_images()
{
$directory = 'wp-content/uploads/';
$directory_stream = @ opendir($directory);
// Display information about the directory stream
//  print_r ($directory_stream);
while ($entry = readdir ($directory_stream)) 
    {
    if (! is_file ("$directory/$entry"))
    continue;
    echo '<img src="wp-content/uploads/'. $entry .'" alt="" />';
    }
}


推荐答案

如果你不回显或连接< img> 并将文件名添加到数组中,你可以轻松生成你想要的标记。

If you don't echo or concatenate the <img> and add the filename to an array you can easily generate the markup you want.

<?php
    $dir = 'wp-content/uploads/';
    $imgs = array();

    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if (!is_dir($file) && preg_match("/\.(bmp|jpe?g|gif|png)$/", $file)) {
                array_push($imgs, $file);
            }
        }

        closedir($dh);
    } else {
        die('cannot open ' . $dir);
    }

    foreach ($imgs as $idx=>$img) {
        $class = ($idx == count($imgs) - 1 ? ' class="last"' : '');
        echo '<img src="' . $dir . $img . '" alt="' . 
             $img . '"' . $class . ' />' . "\n";
    }
?>

这篇关于用于解析目录的PHP脚本,列出所有图像并添加class =&quot; last&quot;到目录中的最后一个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆