我正在制作一个小部件,在用户放置{0}或{5}的地方显示图像。这可以用php完成吗?我不知道从哪里开始,我在谷歌上搜索它已经筋疲力尽了,因为我不知道好的关键词。
是的,可以做到。对于简单的情况,使用str_replace
docs;对于更复杂的情况,则使用preg_replace
docs。
提供的PHP手册链接会告诉你这一切。你可能想先尝试str_replace
,因为它比使用正则表达式简单得多。
// replace all occurrences of "{0}" with "AWESOME"
$str = "{0} --- {0}";
$str = str_replace("{0}", "AWESOME", $str);
echo $str; // outputs: AWESOME --- AWESOME
非常简单地使用字符串替换:
http://php.net/manual/en/function.str-replace.php
$text = str_replace("[0]", "<im>", $text, $count);