如何将“&”替换为& 在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:22:37  来源:igfitidea点击:

how to replace "&" to & in php

phpstring

提问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

链接:http: //php.net/manual/en/function.htmlspecialchars.php

回答by Mohit Bumb

This code is working but maybe you can't see it in browser

此代码有效,但您可能在浏览器中看不到它