doufengsui7449 2014-02-11 15:56
浏览 63
已采纳

php echo无需转义引号

Is there a way to 'echo' / 'render HTML' and not have to escape every quote?

Kind of like how below the HTML below is being rendered without having to use echo or escape anything.

disregard any php syntax errors, example only

 <? if($a == $b) {;?> I said: "It's with quotes" <? ;} ?>
 <? else {;?> They said "It's still with quotes" <? ;} ?>

the reason for this is I need to echo a large Javascript file that uses both single and double quotes, and it would take forever to escape them all.

  • 写回答

4条回答 默认 最新

  • doumeng06063991 2014-02-11 15:59
    关注

    Assuming you're just trying to display (not run) some code, you can use any of these two approaches:

    • Store the JavaScript file as a separate file, and use file_get_contents() to get the code and display it in <pre> code-blocks
    • Use the HEREDOC syntax to store the file contents

    Approach #1:

    $contents = file_get_contents('file.js');
    echo '<pre>', $contents, '</pre>';
    

    Approach #2:

    $contents = <<<FILE
    
    ... contents ...
    
    FILE;
    
    echo '<pre>', $contents, '</pre>';
    

    I'd go with the first approach though. That way, you won't clutter up your script with long JavaScript code that's not actually used for page rendering. It's usually a good idea to separate your logic into multiple files. In both cases though, you don't have to worry about quoting, at all.

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

报告相同问题?

悬赏问题

  • ¥20 求计算赫斯特(Hurst)指数
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大