【问题标题】:PHP Random images with links - how to add text/title?带有链接的 PHP 随机图像 - 如何添加文本/标题?
【发布时间】:2012-10-05 07:38:52
【问题描述】:
<?php
// Random Image With Link
// https://getbutterfly.com/
//
// Usage:
//
// Save this file as ads.php and use the include function to call it inside your web site

function display_random_img($array) {
    $key = rand(0 , count($array) -1);
    $link_url = $array[$key]['url'];
    $alt_tag = $array[$key]['alt'];
    $random_img_url = $array[$key]['img_url'];
    list($img_width, $img_height) = getimagesize($random_img_url);
    return "<a href=\"$link_url\"><img src=\"$random_img_url\" width=\"$img_width\" height=\"$img_height\" alt=\"$alt_tag\" /></a>";
}

// Edit the following values accordingly
$ads_array = array(
    array(
        'url' => 'http://www.google.com/',
        'alt' => 'Google',
        'img_url' => 'images/1.png'
    ),
    array(
        'url' => 'http://www.yahoo.com/',
        'alt' => 'Yahoo!',
        'img_url' => 'images/2.png'
    ),
    array(
        'url' => 'http://www.msn.com/',
        'alt' => 'MSN',
        'img_url' => 'images/3.png'
    )
);
$ads_array_1 = array( // add or remove accordingly
    array(
        'url' => 'http://www.google.com/',
        'alt' => 'Google',
        'img_url' => 'images/1.png'
    ),
    array(
        'url' => 'http://www.yahoo.com/',
        'alt' => 'Yahoo!',
        'img_url' => 'images/2.png'
    ),
    array(
        'url' => 'http://www.msn.com/',
        'alt' => 'MSN',
        'img_url' => 'images/3.png'
    )
);

echo display_random_img($ads_array);
echo display_random_img($ads_array_1); // add or remove accordingly
?>

在互联网上搜索一个可行的 PHP 数组随机化函数时,这就是我最终得到的。它工作得很好,有 url、alt、img_url。 我不知道如何显示文字覆盖/与图像?

【问题讨论】:

    标签: php html image random hyperlink


    【解决方案1】:

    我假设您的意思是悬停文本...将图像标签的标题设置为您要显示的文本

    例如:

    <img title="some hover text" ...
    

    为了顺利完成这项工作,您只需为数组中的每个项目添加一个“标题”元素。

    如果第一个假设是错误的,并且您想要做的是显示一些文本以及图像,请执行以下操作:

    <a href=...>
        <img .../>
        some text or html
    </a>
    

    你可以在锚标签中放任何你想要的东西。

    【讨论】:

      【解决方案2】:

      简单:

      1) 给数组添加标题:

      array(
          'url' => 'http://www.google.com/',
          'alt' => 'Google',
          'img_url' => 'images/1.png',
          'title' => 'Google Title'
      ),
      

      2) 用

      替换你的函数
      function display_random_img($array) {
          $key = rand(0 , count($array) -1);
          $link_url = $array[$key]['url'];
          $alt_tag = $array[$key]['alt'];
          $random_img_url = $array[$key]['img_url'];
          $title = $array[$key]['title'];
          list($img_width, $img_height) = getimagesize($random_img_url);
          return "<a href=\"$link_url\"><img src=\"$random_img_url\" width=\"$img_width\" height=\"$img_height\" alt=\"$alt_tag\" title=\"$title\" /></a>";
      }
      

      如果您希望您的标题位于图像或其他任何内容旁边,只需在 html 中进行相关更正:

      return "$title : <a href=\"$link_url\"><img src=\"$random_img_url\" width=\"$img_width\" height=\"$img_height\" alt=\"$alt_tag\" title=\"$title\" /></a>";
      

      【讨论】:

      • 谢谢!我会尝试这样的事情: return "

        $title

        ";
      猜你喜欢
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      相关资源
      最近更新 更多