【发布时间】:2018-01-24 12:02:30
【问题描述】:
我的 PHP 有点小问题。
$string = "[:string] has [:int] [:string] and [:int] [:string]";
我只想修改(可能用 str_replace)它:
$string = "He has 5 apples and 3 bananas";
我该怎么做?
(经典的 str_replace() 将用相同的单词修改所有 [:int],
这不是我想要的)。
非常感谢...
【问题讨论】:
-
preg_replace_callback -
你应该使用数组
-
使用
sprintf:sprintf('%s has %u %s and %u %s', 'He', 5, 'apples', 3, 'bananas');。 (可选)首先使用str_replace替换占位符:$format = str_replace(['[:string]', '[:int]'], ['%s', '%u'], $string)。 -
我不知道关键字在字符串中的位置。而且我不想显示字符串。
标签: php string replace str-replace keyword