如何将“&”替换为& 在 php 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8294927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to replace "&" to & in php
提问by user989184
I am using this code to replace &
to &
.
我正在使用此代码替换&
为&
.
$arr = array();
$arr['oassade'] = 'one&1';
$arr['asdas'] = 'tw&o2';
$arr['asdsads'] = '&three123';
$columns = array($arr);
foreach ($arr as $key => $value){
$arr[$key] = str_replace("&", "&", $value);
}
print_r($arr);
It is not replacing, but when I change
它不是取代,而是当我改变
$arr[$key] = str_replace("&", "&", $value);
to
到
$arr[$key] = str_replace("&", "1", $value);
It is replacing &
with 1
.
What should I do?
Thanks....
它正在替换&
为1
. 我该怎么办?谢谢....
回答by martincarlin87
You could try using htmlspecialchars()
instead, this should encode the ampersands for you so that you don't have to worry about it.
您可以尝试使用htmlspecialchars()
,这应该为您编码&符号,这样您就不必担心了。
Link: http://php.net/manual/en/function.htmlspecialchars.php
回答by Mohit Bumb
This code is working but maybe you can't see it in browser
此代码有效,但您可能在浏览器中看不到它