我的当前代码:
<?php
$list = 'hello, sentence, banana, apple';
$string = "This is a sentence";
// output should be: This is a ****
$output = str_replace($list, '****', $string);
echo $output;
?>
谁能帮助我输出一个字符串的文本,并从逗号分隔列表中替换单词请?
这应该可以为您工作:
只需将字符串explode()
放入数组中,然后您就可以像使用str_replace()
一样简单地使用它。例如
$output = str_replace(array_map("trim", explode(",", $list)), '****', $string);
array_map()
调用trim()
作为回调,只是为了删除每个数组元素的开始和结束处的所有带空间。