PHP中的Foreach循环,键分配错误

时间:2012-05-14 23:10:24

标签: php foreach

好吧,我承认这是一个枯燥的问题,但我找不到错误。

所以我的代码:

$this->table = array (
          "š" => "š",
          "Š" => "Š",
          "ý" => "ý",
          "Ý" => "Ý",
          "á" => "á",
          "Á" => "Á",
          "í" => "í",
          "Í" => "Í",
          "ú" => "ú",
          "Ú" => "Ú"
        );

$this->keyword = "š Č ú";

foreach ($this->table as $key => $value) {
                echo "key: ".$key." value ".$value." ";
                $this->keyword = str_replace($key, $value, $this->keyword);
            }

所以我想根据数组中的值替换var关键字中的特殊字符,但这不起作用。这部分脚本的输出是:

键:š值š 关键:Š值Š 关键:ý值ý ...

我做错了什么?为什么值$key$value相同?

1 个答案:

答案 0 :(得分:2)

$value上使用htmlentities,否则该实体(例如ú)将由浏览器呈现。

...
echo "key: ".$key." value ".htmlentities($value)." ";
...