【问题标题】:Merge array of alt tags in img array dynamically动态合并img数组中的alt标签数组
【发布时间】:2015-07-24 08:31:22
【问题描述】:

我有一组 alt 标签:

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

现在我想要将这些 alt 标签添加到 img src 动态,即 image 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++;  
                }

提前致谢

【问题讨论】:

  • 为什么是 JavaScript 和 jQuery 标签?
  • 您的代码显示什么错误?你的alt标签没变吗?问题是什么 ?欢迎提供更多信息:)
  • 如果这可以使用 javascript 或 jquery 完成,这就是为什么还要添加 thsee
  • 我尝试的代码多次打印相同的alt标签,这是没有用的。我希望每个图像都应该有自己的 alt 标签,如数组中定义的那样
  • 你确定你的匹配规则是正确的吗?如果您说多次使用相同的 alt 标签,那么我可以看到您的代码为什么不起作用的唯一方法是您的代码将继续;声明过于频繁。

标签: javascript php arrays


【解决方案1】:

首先你的声明是错误的..

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

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

【讨论】:

  • 一样,有什么区别
  • @user3480318 实际上,它不一样。您忘记用 > ("hii) 结束第一个字符串
  • 是的,添加解决了我的问题。我的代码有效
【解决方案2】:

修改了一些代码(在 alts 数组中的 "hii 之后添加缺少的 ",检查 $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++;
}
?>

【讨论】:

    【解决方案3】:
    $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++;  
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 2016-07-23
      相关资源
      最近更新 更多