如果字符串是......然后用图像替换它? [英] If string is ... then replace it with an image?

查看:15
本文介绍了如果字符串是......然后用图像替换它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下一段代码:

<?php echo get_custom_field('Services'); ?>

有三种可能的字符串输出:food"、drink"、food &drink".

There are three possible string outputs for this: "food", "drink", "food & drink".

我想用小图标(例如喝"的玻璃杯)替换这些字符串.

I would like to replace these strings with small icons (e.g. a glass for "drink").

有没有一种简单的方法可以用 PHP 做到这一点?

Is there a simple way of doing this with PHP?

谢谢

推荐答案

您可以通过数组映射值:

You could map the values via an array:

$items = array(
    "Food" => "plate.png",
    "Drink" => "glass.png",
    "Food & Drink" => "tray.png"
);

然后在回显图像时执行查找:

Then perform your look-up when echoing the image:

<img src="<?php echo $items[ get_custom_field('Services') ]; ?>" />

这篇关于如果字符串是......然后用图像替换它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆