【问题标题】:strange string convertion using form helper使用表单助手进行奇怪的字符串转换
【发布时间】:2012-05-15 18:22:25
【问题描述】:

我有以下代码:

<?php 

     $select_options = array();
     foreach($delivery_options as $option)
     {
        $select_options[$option->getDeviceId()] = $option->getDeviceName();
        echo $select_options[$option->getDeviceId()]; 
     }

     echo $this->Form->input('default_device', array( 
        'type' => 'select', 
        'options' => $select_options, 
        'value' => $default_device,
        'label' => '', 
    ));


?>

在 foreach 循环中,每个回显都会返回: abc'abc 在 html 源代码中它看起来像这样 abc&amp;amp;#39;abc

然后在选择输入:abc&amp;amp;#39;abc html源代码中:abc&amp;amp;#39;abc

这意味着 abc&amp;amp;#39;abc 中的 &amp; 字符被转换为它的 html 编码 - &amp;amp - 但它是怎么发生的?

我也尝试了 htmlentities() 和 htmlspecialchars() 但这仍然没有帮助...

【问题讨论】:

  • 在这样的输入中使用值是错误的。您销毁帖子上的表格(它会一直重置)。使用“默认”正确设置默认值,或者使用控制器更好地设置默认值 - 请参阅dereuromark.de/2010/06/23/working-with-forms
  • true,但在这种情况下,$default_device 不是选择的默认值,而是用户的默认设备 - 我从 $this-Auth-user('default_device') 读取它...

标签: html cakephp encoding cakephp-2.0


【解决方案1】:

select 输入类型允许一个特殊的 $option 属性,称为 'escape' 接受布尔值并确定是否为 HTML 实体 编码选择选项的内容。默认为真。

尝试将其设置为 false。

echo $this->Form->input('default_device', array( 
        'type' => 'select', 
        'options' => $select_options,
        'escape' => false, // like so
        'value' => $default_device,
        'label' => '', 
));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    相关资源
    最近更新 更多