php替换字符串中数组中所有出现的键 [英] php replace all occurances of key from array in string

查看:26
本文介绍了php替换字符串中数组中所有出现的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是重复的,但我没有找到好的解决方案.

maybe this is duplicate but i did't find good solution.

我有数组

$list = Array
(
   [hi] => 0
   [man] => 1
);
$string="hi man, how are you? man is here. hi again."

它应该产生 $final_string = "0 1,你好吗?1 来了.0 又来了."

我怎样才能以聪明的方式实现它?非常感谢.

How can I achieve it with smart way? Many thanks.

推荐答案

我的头顶:

$find       = array_keys($list);
$replace    = array_values($list);
$new_string = str_ireplace($find, $replace, $string);

这篇关于php替换字符串中数组中所有出现的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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