如何用一个字符串替换某个字符为它使用php?
问题描述:
我有一个问题,我可以用#
替换字符串中的某个字符,如#
。 我有一个数组中的所有字符检查器和替换器。像这样 -如何用一个字符串替换某个字符为它使用php?
$string_check = array(
"#" => "#",
.... and so on (list is too big)
);
那么我该如何做这件事。请帮助我。我只有20天的PHP经验。
答
str_replace
正是如此,它也接受数组作为置换贴图:
$string_check = array(
"#" => "#"
);
$result = str_replace (array_keys($string_check), array_values($string_check), $original);
+0
感谢它也为我工作好!非常感谢! – 2011-03-16 12:00:47
答
$search = array('hello','foo');
$replace = array('world','bar');
$text = 'hello foo';
$result = str_replace($search,$replace,$text);
// $result will be 'world bar'
但在你的情况下,它看起来像某种编码,你有没有试过htmlspecialchars
?
感谢它的工作就像我想的一样!再次感谢! – 2011-03-16 11:59:33