繁体   English   中英

动态合并img数组中的alt标签数组

[英]Merge array of alt tags in img array dynamically

我有一个alt标签数组:

$array_alt=array("hii,
            "Best",
            "abc",
            //"board certified",
            "xyz",
            "hello",
            "new"
            )

现在我想要的是将这些alt标签动态地添加到img src ,即对于图像1 alt标签应该为“ hii” ,其余的都相同。

这是通过文件夹动态调用图像并且所有图像都出现在滑块中的方式:

$images = scandir(images/all_images); // get path 
            sort($images,1); // 1 is to sort images numerically

            foreach($images as $img)
                { 
                    if($img === '.' || $img === '..')
                    {
                        continue;
                    }   
                    // check extensions as we need only images
                    if ((preg_match('/.jpg/',$img))  ||  (preg_match('/.gif/',$img)) || (preg_match('/.tiff/',$img)) || (preg_match('/.png/',$img)) )
                    {               
                        list($width, $height, $type, $attr) = getimagesize($path.$img);

                        if(($width<$height) || ($height>500) )
                        {
                        ?>
                            <div class="item img-landscape"><img class="lazyOwl" alt="" data-src="<?php echo $path.$img; ?>" ></div>
                           <?php
                        }
                        else{
                        ?>
                            <div class="item"><img class="lazyOwl" alt="" data-src="<?php echo $path.$img; ?>" ></div>
                           <?php
                        }
                    }
                    else
                    {
                        continue;
                    }
                }

请任何人能帮助我。

我尝试使用此功能,但似乎无济于事。

$images = scandir($img_path);
            sort($images,1); // 1 is to sort images numerically

            $i=0;
            foreach($images as $img)
                { 

                    echo $array_alt[$i]."<br>";
                    if($img === '.' || $img === '..')
                    {
                        continue;
                    }   
                    // check extensions as we need only images
                    if ((preg_match('/.jpg/',$img))  ||  (preg_match('/.gif/',$img)) || (preg_match('/.tiff/',$img)) || (preg_match('/.png/',$img)) )
                    {               
                        list($width, $height, $type, $attr) = getimagesize($path.$img);

                        if(($width<$height) || ($height>500) )
                        {
                        ?>
                            <div class="item img-landscape"><img class="lazyOwl" alt="<?php echo $array_alt[$i]?>" data-src="<?php echo $path.$img; ?>" ></div>
                           <?php
                        }
                        else{
                        ?>
                            <div class="item"><img class="lazyOwl" alt="<?php echo $array_alt[$i]?>" data-src="<?php echo $path.$img; ?>" ></div>
                           <?php
                        }
                    }
                    else
                    {
                        continue;
                    }
                   $i++;  
                }

提前致谢

首先,您的声明是错误的。

$array_alt=array("hii,
        "Best",
        "abc",
        //"board certified",
        "xyz",
        "hello",
        "new"
        )

将会

$array_alt=array("hii",
        "Best",
        "abc",
        //"board certified",
        "xyz",
        "hello",
        "new"
        )

修改了您的代码(在alts数组中的"hii "之后添加缺少的" ,检查$array_alt是否存在键,简化了扩展名的检查),它似乎对我$array_alt

<?php

$array_alt = array(
    "hii",
    "Best",
    "abc",
    "board certified",
    "xyz",
    "hello",
    "new"
);

$path = '/public_path/';
$images = scandir($img_path);
sort($images, SORT_NUMERIC); // SORT_NUMERIC == 1 is to sort images numerically

$allowedExtensions = array('jpg', 'gif', 'tiff', 'png');
$i = 0;
foreach ($images as $img) {
    if (isset($array_alt[$i])) {
        $alt = $array_alt[$i];
    } else {
        $alt = "default_alt_value";
    }
    echo $alt . "<br>";
    if ($img === '.' || $img === '..') {
        continue;
    }
// check extensions as we need only images
    if (!in_array(pathinfo($img, PATHINFO_EXTENSION), $allowedExtensions)) {
        continue;
    }

    list($width, $height, $type, $attr) = getimagesize($path . $img);

    if (($width < $height) || ($height > 500)) {
        ?>
        <div class="item img-landscape"><img class="lazyOwl" alt="<?php echo $alt ?>"
                                             data-src="<?php echo $path . $img; ?>"></div>
    <?php
    } else {
        ?>
        <div class="item"><img class="lazyOwl" alt="<?php echo $alt ?>"
                               data-src="<?php echo $path . $img; ?>"></div>
    <?php
    }
    $i++;
}
?>
$array_alt=array("hii",
        "Best",
        "abc",
        //"board certified",
        "xyz",
        "hello",
        "new"
        );

$img_path="images/slider";
$path=JURI::BASE().$img_path."/"; // url/path of the images
$images = scandir($img_path);
sort($images,1); // 1 is to sort images numerically

    $i=0;
    foreach($images as $img)
    { 
       if($img === '.' || $img === '..')
       {
           continue;
       }   
       // check extensions as we need only images
       if ((preg_match('/.jpg/',$img))  ||  (preg_match('/.gif/',$img)) || (preg_match('/.tiff/',$img)) || (preg_match('/.png/',$img)) )

                {               
                    list($width, $height, $type, $attr) = getimagesize($path.$img);

                    if(($width<$height) || ($height>500) )
                    {
                    ?>
                        <div class="item img-landscape"><img class="lazyOwl" alt="<?php echo $array_alt[$i]?>" data-src="<?php echo $path.$img; ?>" ></div>
                       <?php
                    }
                    else{
                    ?>
                        <div class="item"><img class="lazyOwl" alt="<?php echo $array_alt[$i]?>" data-src="<?php echo $path.$img; ?>" ></div>
                       <?php
                    }
                }
                else
                {
                    continue;
                }
               $i++;  
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM