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条)

报告相同问题?