简体   繁体   English

str_replace与数组

[英]str_replace with arrays

$a = $_GET['a'];
$d = array("æ", "ø", "å", "Æ", "Ø", "Å");
$e = array("&aelig", "&oslash", "&aring", "&AElig", "&Oslash", "&Aring");
$new = str_replace("$d","$e","$a");
echo $new;

Can anybody tell me what I'm doing wrong here. 谁能告诉我我在做什么错。 It works perfectly fine if I just write: 如果我只写:

$a = $_GET['a'];
new = str_replace("ø","&oslash","$a");
echo $new;

Then atleast the "ø" is replaced, but I really want to do it with two arrays instead of replacing one letter at a time, 6 times... 然后至少要替换“ø”,但是我真的想用两个数组来代替而不是一次替换一个字母6次...

$new = str_replace($d, $e, $a);

You're passing variables, not a strings that consist of variables. 您传递的是变量,而不是包含变量的字符串。

If you compare outputs in this code 如果您在此代码中比较输出

$a = array(123);
var_dump($a);
var_dump("$a");

you'll notice, that the first one is an array, and the second one is a string with array implicitly casted to a string. 您会注意到,第一个是数组,第二个是带有隐式转换为字符串的数组的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM