douyi6168 2014-10-19 21:40
浏览 253

将对象数组打印到HTML数据属性中

I'm hoping someone can give me some advice to this problem. I have to print out an array of objects to an HTML data attribute in the format below...

[{value:'some_val', text:'some_val'}, {value:'some_val', text:'some_val'}]

My PHP code generates this is a string as below...

$obj = "[";
    foreach ( $vals AS $key=>$v ){
                $key = addslashes( htmlentities($key) );
                $v = addslashes( htmlentities($v) );
                $obj .= "{ value:'{$key}',text:'{$v}' },";
            }
  $obj = rtrim($obj, ",") . "]";

This worked fine until the values had double quotes and single quotes in them which is why I added addslashes() and htmlentities().

A Sample of data that I print out that breaks my HTML is below. When the data is sent to the database its fine but as the data is retrieved from the database I end up with an extra single quote on the string, in turn this doesn't match my option value etc etc.

Sample data ( from firebug so no entities )...

[{ value:'< 5\' (< 152 cm)',text:'< 5\' (< 152 cm)' },{ value:'5\'0" (152 cm)',text:'5\'0" (152 cm)' },{ value:'5\'1" (155 cm)',text:'5\'1" (155 cm)' },{ value:'5\'2" (157 cm)',text:'5\'2" (157 cm)' },{ value:'5\'3" (160 cm)',text:'5\'3" (160 cm)' },{ value:'5\'4" (163 cm)',text:'5\'4" (163 cm)' },{ value:'5\'5" (165 cm)',text:'5\'5" (165 cm)' },{ value:'5\'6" (168 cm)',text:'5\'6" (168 cm)' },{ value:'5\'7" (170 cm)',text:'5\'7" (170 cm)' },{ value:'5\'8" (173 cm)',text:'5\'8" (173 cm)' },{ value:'5\'9" (175 cm)',text:'5\'9" (175 cm)' },{ value:' 5\'10" (178 cm)',text:' 5\'10" (178 cm)' },{ value:'5\'11" (180 cm)',text:'5\'11" (180 cm)' },{ value:'6\'0" (183 cm)',text:'6\'0" (183 cm)' },{ value:'6\'1" (185 cm)',text:'6\'1" (185 cm)' },{ value:'6\'2"(188 cm)',text:'6\'2"(188 cm)' },{ value:'6\'3" (191 cm)',text:'6\'3" (191 cm)' },{ value:'6\'4" (193 cm)',text:'6\'4" (193 cm)' },{ value:'6\'5" (196 cm)',text:'6\'5" (196 cm)' },{ value:'6\'6" (198 cm)',text:'6\'6" (198 cm)' },{ value:'6\'7" (201 cm)',text:'6\'7" (201 cm)' },{ value:'6\'8" (203 cm)',text:'6\'8" (203 cm)' },{ value:'6\'9" (206 cm)',text:'6\'9" (206 cm)' },{ value:' 6\'10" (208 cm)',text:' 6\'10" (208 cm)' },{ value:'6\'11" (211 cm)',text:'6\'11" (211 cm)' },{ value:'7\' 0" (213 cm)',text:'7\' 0" (213 cm)' },{ value:'>7\' (> 213 cm)',text:'>7\' (> 213 cm)' }]

This would be in a data-source="" attribute.

Value returned from database would be like this 5'3" (160 cm)' - see the extra single quote but this doesn't appear in the database.

Any suggestions to fix or do this another way better would be a big help.

Thanks in advance

  • 写回答

2条回答 默认 最新

  • drq9991 2014-10-19 21:49
    关注

    Try adding the ENT_QUOTES on the htmlentities($v) like so: htmlentities($v, ENT_QUOTES). That should convert your quotes so they are not interfering with the HTML. When you processed it (convert it back you have to have the ENT_QUOTES on the decode.

    This is the result:

    <?php
    $v  =   "5'3\" (160 cm)";
    echo htmlentities($v, ENT_QUOTES);
    ?>
    

    CONVERSION:

    5&#039;3&quot; (160 cm)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)