doubi2228 2014-12-18 21:10
浏览 29
已采纳

使用PHP和HTML转义字符串

I have these two lines that I would like to escape the $type variable:

$functionName = str_replace('-', '_', $type);
$output .= '<div class="tab-pane" id="'. $type .'">';

I tried escaping like below but its confusing me and not sure whether thats right:

$output .= '<div class="tab-pane" id="\'. $type .'\">';
  • 写回答

3条回答 默认 最新

  • dongtaihui5131 2014-12-18 21:24
    关注

    Example 1: Variable between single quotes

    If you use single quotes everything between them will always be treated as part of the string.

    $output .= '<div class="tab-pane" id="' . $type . '">";

    Example 2: Variable between double quotes (option 1)

    If you have a variable that you want to pass in a string you can just put it in there if you use double quotes and de variable is nog 'touching' the other words. It should always have spaces.

    $output .= "<p>i would like to $your_text_here with you.</p>";
    

    Example 3: Escaping quotes in a string

    Escaping characters in a string can be done by using a \ (backslash) before the character you want to escape.

    $output .= "<div class=\"tab-pane\" id=\"example-id\">";
    

    Example 4: Variable between double quotes without spaces next to it

    You can place your variable between {} braces if you use double quotes (option 2)

    $output .= "<div class=\"tab-pane\" id=\"{$type}\">";
    

    This question was however already answered in Mixing PHP variable with string literal

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

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号