我想用图像替换页面上{0}的所有实例,或者在打印出图像时运行函数.我可以用php做这件事吗


I want to replace all instances of {0} on a page with an image or run a function when that is printed out. can i do this with php?

我正在制作一个小部件,在用户放置{0}或{5}的地方显示图像。这可以用php完成吗?我不知道从哪里开始,我在谷歌上搜索它已经筋疲力尽了,因为我不知道好的关键词。

是的,可以做到。对于简单的情况,使用str_replacedocs;对于更复杂的情况,则使用preg_replacedocs

提供的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);