【问题标题】:Images ALT tag in a text file retrieve by php由 php 检索文本文件中的图像 ALT 标记
【发布时间】:2014-03-17 09:36:04
【问题描述】:

我正在尝试基于david walsh's howto. 设置一个小画廊

目前,此图库提供使用图像文件名的 alt 标记。我需要一种方法来使用与我的图像相关联的文本文件,如下所示:

myprettypicture.jpg = "Here's a pretty picture"  
mynotsoprettypicture.jpg = "Here's a not so pretty picture"  

然后检索它以将其作为 ALT 标记与 php 库一起放置。到目前为止,我已经查看了几个画廊,其中有几个在使用这种方法,但我不知道如何在我的实际画廊中使用它...
你知道脚本或简单方法吗?我在寻找什么?

谢谢!

【问题讨论】:

    标签: php image tags alt


    【解决方案1】:

    如果我对你的理解正确,你有一个 source_file.htm,如下所示。

    <!DOCTYPE html>
    <html>
    <body>
    <img src="myprettypicture.jpg" alt="myprettypicture.jpg" height="50" width="50">
    <img src="mynotsoprettypicture.jpg" alt="mynotsoprettypicture.jpg" height="50" width="50">
    </body>
    </html>
    

    现在,最好将替换图像的 alt 标签存储在 images.ini 这样的文件中。

    myprettypicture.jpg = "Here's a pretty picture"
    mynotsoprettypicture.jpg = "Here's a not so pretty picture"
    

    现在你可以运行这个 PHP 脚本了:

    <?php
    $ini_array = parse_ini_file("images.ini"); // load "images.ini" into array
    foreach($ini_array as $key => $val)
    {
      $img[] = 'alt="' . $key . '"';
      $alt[] = 'alt="' . $val . '"';
    }
    $source = file_get_contents('source_file.htm'); // get Your "source_file.htm"
    $target = str_replace($img, $alt, $source);     // make all required replacements
    file_put_contents('target_file.htm', $target);  // save the result to "target_file.htm"
    ?>
    

    获取target_file.htm

    <!DOCTYPE html>
    <html>
    <body>
    <img src="myprettypicture.jpg" alt="Here's a pretty picture" height="50" width="50">
    <img src="mynotsoprettypicture.jpg" alt="Here's a not so pretty picture" height="50" width="50">
    </body>
    </html>
    

    参考: str_replaceparse_ini_filefile_get_contentsfile_put_contents

    【讨论】:

    • 这正是我正在寻找的东西:图像文件夹的某种 .ini 包装器。非常感谢您提供的 cmets,非常有用。
    猜你喜欢
    • 2015-08-13
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    相关资源
    最近更新 更多