douya1974 2012-05-14 23:10
浏览 46
已采纳

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

OK, I admit this is a dull question but I cant find mistake.

So my code:

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

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

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

So I want to replace specials chars in var keyword according values in array, but this aint working. Output from this part of script is:

key: š value š key: Š value Š key: ý value ý ...

What am I doing wrong? Why is value $key same as $value?

  • 写回答

1条回答 默认 最新

  • dongtaigan1594 2012-05-14 23:14
    关注

    Use htmlentities on $value, otherwise the entity (e.g. ú) will be rendered by the browser.

    ...
    echo "key: ".$key." value ".htmlentities($value)." ";
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?