简体   繁体   中英

Printing out array of objects into a HTML data attribute

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


You can use the PHP function json_encode($array) to do that, it will escape all specials chars, and decoded correctly in client side
Hope it helps

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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