tags:

views:

126

answers:

3

I am trying to generate a specific link and accompanying html depednant on the existance of a file. The code I am using to do so:

if(file_exists('../images/'. $pk . '.jpg'))
    {
    $imageSrc = "../images/". $pk . ".jpg";
    $imagehtml = htmlentities(json_encode("<img src=\"".$imageSrc."\" >"));
    $screenshotLink =  "<p><a href=\"#\" onclick=\"makewindows(\"$imagehtml\"); return false;\">View Screenshot</a>";
       } 
else {
    $screenshotLink = '';
     }

This results in the following, useless html:

<a href="#" onclick="makewindows(" &quot;&lt;img="" src="%5C%22..%5C/images%5C/160329461329.jpg%5C%22" &gt;&quot;="" );="" return="" false;="">View Screenshot</a>

I don't understand this, because the above is essentialy the same code as:

$html = htmlentities(json_encode($ARTICLE_DESC));

$imagehtml = htmlentities(json_encode("<img src='".$imageSrc."' >"));

echo "<a href='#' onclick=\"makewindows(" . $imagehtml . "); return false;\">

<img src='".$imageSrc."' width='".$imageSize["width"]."' height='".$imageSize["height"]."'></a>

<p><a href='#' onclick=\"makewindows(" . $html . "); return false;\">Click for full description </a></p>";

which produces the following html which works fine:

<a href="#" onclick='makewindows("<img src=\"..\/images\/160329461329.jpg\" >"); return false;'>
<img src="../images/160329461329.jpg" width="199" height="300"></a>

I know it has something to do with quotes, but I am not sure what exactly.

+6  A: 

Try this:

$imagehtml = htmlspecialchars(json_encode("<img src=\"".$imageSrc."\" >"), ENT_QUOTES);
$screenshotLink =  "<p><a href=\"#\" onclick=\"makewindows($imagehtml); return false;\">View Screenshot</a>";
Gumbo
why did changing the quotes around as you have work? Why was my attempt failing?
Joshxtothe4
It now generaes this: <a onclick="makewindows("<img src=\"..\/images\/250397542564.jpg\" >"); return false;" href="#">View Screenshot</a>
Joshxtothe4
A: 

Lookup the ENT_NOQUOTES parameter in the php manual

And htmlspecialchars() != htmlentities() btw.

OderWat
A: 
$imagehtml = htmlspecialchars(json_encode('<img src="'.$imageSrc.'" >'), ENT_QUOTES);
$screenshotLink = '<p><a href="#" onclick="makewindows($imagehtml); return false;">View Screenshot</a>';

Why not use ticks?

Droo