dongpan8928 2009-04-30 12:21
浏览 55
已采纳

在PHP问题的HTML中引用

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.

  • 写回答

3条回答 默认 最新

  • dongnianchou7047 2009-04-30 15:02
    关注
    $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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画