如果使用特定单词,php 将图像添加到标题

发布于 2024-12-28 00:38:44 字数 299 浏览 2 评论 0原文

我有以下代码:

<h2>
  <?php echo $this->item->title; ?>
</h2>

根据作为标题引入的单词,我需要在标题文本的左侧放置不同的图像。

图片:uk.png、france.png、germany.png

因此,如果标题文本说“法国”,我需要拉入 france.png 图像。因此,我需要“可以”使用的图像和标题列表,如果标题与图像不匹配,则不会显示图像。

希望这是有道理的......

I have the following code:

<h2>
  <?php echo $this->item->title; ?>
</h2>

Dependent on the word being pulled in as the title, I need to place a different image to the left of the title text.

Images: uk.png, france.png, germany.png

So if the title text says France, I need to pull in the france.png image. So I need a list of images and titles that 'could' be used and if a title doesn't match an image, no image is shown.

Hope this makes sense...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你又不是我 2025-01-04 00:38:44
<?php

  $images = array (
    'France' => 'france.png',
    'UK' => 'uk.png',
    'Germany' => 'germany.png'
  );

  if (isset($images[$this->item->title])) {
?>

<img src="<?php echo $images[$this->item->title]; ?>" />

<?php } ?>

<h2>
<?php echo $this->item->title; ?>
</h2>
<?php

  $images = array (
    'France' => 'france.png',
    'UK' => 'uk.png',
    'Germany' => 'germany.png'
  );

  if (isset($images[$this->item->title])) {
?>

<img src="<?php echo $images[$this->item->title]; ?>" />

<?php } ?>

<h2>
<?php echo $this->item->title; ?>
</h2>
木槿暧夏七纪年 2025-01-04 00:38:44

例如:

function getPic($title)
{
    static $pics = array('uk'      => 'uk.png',
                         'france'  => 'france.png',
                         'germany' => 'germany.png');
    return isset($pics[$title]) ? "<img src='{$pics[$title]}' >" : "";
}

for example:

function getPic($title)
{
    static $pics = array('uk'      => 'uk.png',
                         'france'  => 'france.png',
                         'germany' => 'germany.png');
    return isset($pics[$title]) ? "<img src='{$pics[$title]}' >" : "";
}
妥活 2025-01-04 00:38:44

您可以编写一个基于输入字符串返回图像文件的函数,根据您的示例,我们假设为 $this->item->title。其主要目的是在从输入字符串确定“国家/地区”后返回一个字符串。

function getCountryImage($input)
{
    // an array containing the mapping of country names to image file names
    $images = array(
        'France' => 'france.png',
        'UK' => 'uk.png',
        'Germany' => 'germany.png' );

    for each( $images as $country => $filename )
        if( $country == $input )
            return $filename;

    return '';
}

如果要显示图像,唯一剩下的就是:

<img src="[image path]/<?php echo getCountryImage($this->item->title);?>" />

祝你有美好的一天。

You could write a function which returns the image file, based on an input string, which based on your example, we assume is $this->item->title. Its main purpose is to return a string, after determining the 'country' from the input string.

function getCountryImage($input)
{
    // an array containing the mapping of country names to image file names
    $images = array(
        'France' => 'france.png',
        'UK' => 'uk.png',
        'Germany' => 'germany.png' );

    for each( $images as $country => $filename )
        if( $country == $input )
            return $filename;

    return '';
}

The only thing left if to display the image:

<img src="[image path]/<?php echo getCountryImage($this->item->title);?>" />

Have a great day.

铁憨憨 2025-01-04 00:38:44

这可能不是您问题的正确答案,但对于链接,您只需使用 css 即可实现您想要的效果。

a[href$='.zip'], a[href$='.rar'], a[href$='.gzip'] {
    background:transparent url(../images/zip.png) center left no-repeat;
    display:inline-block;
    padding-left:20px;
    line-height:18px;
}

请参阅web-kreation 了解更多示例。

只是我想添加这一点,因为对于任何想要根据链接部分向链接添加图标的人来说,这是一个很好的提示,这与您想要的有点相似。

This may not be the correct answer for your question, but for links you can achive what you want using just css.

a[href$='.zip'], a[href$='.rar'], a[href$='.gzip'] {
    background:transparent url(../images/zip.png) center left no-repeat;
    display:inline-block;
    padding-left:20px;
    line-height:18px;
}

See web-kreation for more examples.

Just tought I'd add this as it's a good tip for anyone wanting to add icons to links depending on parts of the link, which is somewhat similar to what you wanted.

诠释孤独 2025-01-04 00:38:44

使用国家名称作为显示相应图像的键可能被认为不是非常可靠的解决方案。

我建议使用 ISO 3166-1 国家/地区代码作为键。然后,根据国家/地区代码,您可能有两个返回国家/地区名称和图像的函数。

您可能会遇到类似的情况(我在这个示例中没有故意使用类和错误处理):

<?php
function getCountryNameByIsoCode($iso_code)
{
    static $country_names = array ("FRA" => "France", "GBR" => "United Kingdom", ...);
    return $country_name[$code];
}

function getCountryFlagImageFileNameByIsoCode($code)
{
    static $country_flags = array ("FRA" => "france.png", "GBR" => "uk.png", ...);
    return $country_flags[$iso_code];
}
?>

<h2>
    <img src="/img/<?php echo getCountryFlagImageFileNameByIsoCode($this->item->iso_code); ?>" alt="" />
    <?php echo getCountryNameByIsoCode($this->item->iso_code); ?>
</h2>

Usage of the country name as a key to display the corresponding image may be considered as not very reliable solution.

I'd recommend to use ISO 3166-1 country codes as keys. Then, based on the coutry code you might have two functions which return country name and an image.

You may come up to someting like that (I didn't use classes and error handling in this example intentionally):

<?php
function getCountryNameByIsoCode($iso_code)
{
    static $country_names = array ("FRA" => "France", "GBR" => "United Kingdom", ...);
    return $country_name[$code];
}

function getCountryFlagImageFileNameByIsoCode($code)
{
    static $country_flags = array ("FRA" => "france.png", "GBR" => "uk.png", ...);
    return $country_flags[$iso_code];
}
?>

<h2>
    <img src="/img/<?php echo getCountryFlagImageFileNameByIsoCode($this->item->iso_code); ?>" alt="" />
    <?php echo getCountryNameByIsoCode($this->item->iso_code); ?>
</h2>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文