Joshxtothe4
Joshxtothe4

Reputation: 4193

quotes in html in php problem

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.

Upvotes: 1

Views: 605

Answers (3)

Droo
Droo

Reputation: 3205

$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?

Upvotes: 1

OderWat
OderWat

Reputation: 5709

Lookup the ENT_NOQUOTES parameter in the php manual

And htmlspecialchars() != htmlentities() btw.

Upvotes: 0

Gumbo
Gumbo

Reputation: 655229

Try this:

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

Upvotes: 7

Related Questions