duannaoye0732 2016-10-21 10:03 采纳率: 100%
浏览 30
已采纳

php绑定引号在html中

I have following code in php where I am binding html code:

 $my_doc.="<img src='mysite.in/$myDoc->my_doc_name' onclick='window.open(".$base_image_url.$myDoc->my_doc_name.")' value='".$myDoc->my_doc_id."' class='cropcss'/>";

It produces following output:

<img src="mysite.in/8422_1477013411.png" onclick="window.open(mysite.in/8422_1477013411.png)" value="623" class="cropcss">

In window.open(mysite.in/8422_1477013411.png) I have missed single quote inside. How do I add this?

  • 写回答

3条回答 默认 最新

  • dongyan5239 2016-10-21 10:06
    关注

    I prefer to use single quotes for outputting variables like HTML to the browser. Simply use \' to escape a single quote, and pass it to the browser. Something like this should work:

    $my_doc .= '<img src="mysite.in/' . $myDoc->my_doc_name . '" onclick="window.open(\'' . $base_image_url . $myDoc->my_doc_name . '\')" value="' . $myDoc->my_doc_id . '" class="cropcss" />";
    

    If you insist on using double quotes, a minor quick change can fix this (Just add \" around your text):

    $my_doc.="<img src='mysite.in/$myDoc->my_doc_name' onclick='window.open(\"".$base_image_url.$myDoc->my_doc_name."\")' value='".$myDoc->my_doc_id."' class='cropcss'/>";
    

    For more info, see this post on how to escape quotation marks in php

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

报告相同问题?

悬赏问题

  • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
  • ¥15 SPSS分类模型实训题步骤
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的