如果使用特定单词,php 将图像添加到标题
我有以下代码:
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
例如:
for example:
您可以编写一个基于输入字符串返回图像文件的函数,根据您的示例,我们假设为
$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.The only thing left if to display the image:
Have a great day.
这可能不是您问题的正确答案,但对于链接,您只需使用 css 即可实现您想要的效果。
请参阅web-kreation 了解更多示例。
只是我想添加这一点,因为对于任何想要根据链接部分向链接添加图标的人来说,这是一个很好的提示,这与您想要的有点相似。
This may not be the correct answer for your question, but for links you can achive what you want using just css.
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.
使用国家名称作为显示相应图像的键可能被认为不是非常可靠的解决方案。
我建议使用 ISO 3166-1 国家/地区代码作为键。然后,根据国家/地区代码,您可能有两个返回国家/地区名称和图像的函数。
您可能会遇到类似的情况(我在这个示例中没有故意使用类和错误处理):
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):