I was trying to get away from using PHP’s htmlentities and here’s where I stopped:
<?php
echo '<img ... onclick="MP.view(\''.$i->name.'\') />';
?>
But then I thought, instead of doing replaces and checks for special characters, I’ll just JSON the entire object.
<?php
echo '<img ... onclick="MP.view('.json_encode($i).') />';
?>
And this provided a much undesired result putting in a ton of double quotation marks.
So how should I do this? Should I assign a numerical unique id to every image and just pass the id, and then look up the rest of the data from a JS array?
The correct approach in such cases would be:
htmlspecialcharsturns any double quotes into the proper HTML escapes, making the resulting string suitable for most attributes. TheENT_QUOTESparameter also takes care of single quotes; but you probably don’t need that in your example.