note 69342 added to function.json-encode
| From: | RQuadling at GMail dot com | Date: | Fri, 01 Sep 2006 12:40:57 +0000 |
| Subject: | note 69342 added to function.json-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-116585@lists.php.net to get a copy of this message | ||
In trying to get an array which contains &, " ' in it into a JS select took me a
little while.
My solution is a combination of htmlentites() and addslashes(), just like any other sort of data
output.
The example code takes an array and creates a JS object (as the PHP variable is an associative
array).
I also use both single and double quotes in the eval statement to prove resilience.
See http://rquadling.php1h.com/ for this and other usefull
info.
<?php
// Define array with missing indexes.
$a_List = array
(
0 => 'Examine Locks',
1 => 'Get Grade',
2 => 'Get Manufacturer',
3 => "Get Manufacturer's Pattern", // unescaped '
6 => 'Get Mould Number',
7 => 'Validate A&R', // & will become &
'Quote' => '"The answer is : 42".', // " will becode
"
'Last' => 'Quit Process',
);
// Make sure any HTML Entities are not sent unescaped.
foreach($a_List as $m_Key => $m_Value)
{
$a_List[$m_Key] = htmlentities($m_Value);
}
// JSON Encode the list
$s_JSON_Encoded = addslashes(json_encode($a_List));
// Get the list into simple variable for printing.
$s_List = var_export($a_List, True);
// Output the results along with some JS to show what was received.
echo <<< END_TEXT
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"
xml:lang="en">
<head>
<title>JSON test</title>
</head>
<body>
<div>Original Data : <pre>$s_List</pre></div>
<div>JSON Encoded : <pre>$s_JSON_Encoded</pre></div>
<div>JSON Decoded : <pre><script language="JavaScript"
type="text/javascript">
var obj_dq = eval("($s_JSON_Encoded)");
var obj_sq = eval('($s_JSON_Encoded)');
var s_dq = '';
var s_sq = '';
for(var s_label in obj_dq)
{
s_dq = s_dq + '\\n' + s_label + ' => ' + obj_dq[s_label] + ',';
}
for(var s_label in obj_sq)
{
s_sq = s_sq + '\\n' + s_label + ' => ' + obj_sq[s_label] + ',';
}
document.writeln(s_dq + '\\n' + s_sq);
</script></pre></div>
</body>
</html>
END_TEXT;
?>
----
Server IP: 83.138.144.80
Probable Submitter: 83.104.121.66 (proxied: 1.1 BANDEXCH)
----
X-Spam-Status: No, hits=4.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_30_40,HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.json-encode.php
Edit -- https://master.php.net/note/edit/69342
Del: integrated -- https://master.php.net/note/delete/69342/integrated
Del: useless -- https://master.php.net/note/delete/69342/useless
Del: bad code -- https://master.php.net/note/delete/69342/bad+code
Del: spam -- https://master.php.net/note/delete/69342/spam
Del: non-english -- https://master.php.net/note/delete/69342/non-english
Del: in docs -- https://master.php.net/note/delete/69342/in+docs
Del: other reasons-- https://master.php.net/note/delete/69342
Reject -- https://master.php.net/note/reject/69342
Search -- https://master.php.net/manage/user-notes.php