【问题标题】:PHP trying to escape single quotesPHP试图转义单引号
【发布时间】:2014-01-02 14:51:20
【问题描述】:

我正在创建一个音乐播放器。但是 PHP 只是弄乱了其中包含单引号的文件名。我该如何解决这个问题?

第一个代码是我当前的 PHP。 第二个代码是我想要的输出。

// integer starts at 0 before counting
$i = 0; 
$dir = 'music/';
if ($handle = opendir($dir)) {
    while (($file = readdir($handle)) !== false) {
        if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) 
        echo "<span class='song' id='".$file."' onClick='playSong(\"".addslashes($file)."\");'>".$file."</span><br />";
    }
}

一开始我是做 HTML,但现在我想用 PHP,所以我可以把歌曲拖到一个文件夹中,它们会自动添加到列表中。

这就是 HTML 的样子,这就是我想要的 PHP 输出的样子。

<span id="Martin Garrix - Animals" onClick="playSong('Martin Garrix - Animals');">Martin Garrix - Animals</span>
<br />
<span id="TryHardNinja - Doin' it grand" onClick="playSong('TryHardNinja - Doin\' it grand');">TryHardNinja - Doin' it grand</span>
<br />
<span id="TryHardNinja - Calling All Ghosts" onClick="playSong('TryHardNinja - Calling All Ghosts');">TryHardNinja - Calling All Ghosts</span>

【问题讨论】:

    标签: javascript php html


    【解决方案1】:

    只需更改引号:

    echo '<span class="song" id="'.$file.'" onClick="playSong(\''.addslashes($file).'\');">'.$file.'</span><br />';
    

    PS:我也不确定,如果你需要addslashes

    【讨论】:

    • 非常感谢您的快速遮阳篷,我为不早点尝试而感到愚蠢。将在 5 分钟内使其成为可接受的遮阳篷(不能在这么短的时间内接受遮阳篷)
    【解决方案2】:

    使用 HTML ASCII 码作为单引号:

    &#39;
    

    http://www.ascii.cl/htmlcodes.htm

    【讨论】:

      【解决方案3】:

      你说:

      但是 PHP 只是弄乱了包含单引号的文件名。

      但是您的代码本身是通过addslashes 在此行中添加斜杠:

      echo "<span class='song' id='".$file."' onClick='playSong(\"".addslashes($file)."\");'>".$file."</span><br />";
      

      查看您的 HTML,您添加 addslashes 的原因似乎与您的 HTML 标记参数(即:class='song' 等)有单引号有关。因此,我建议您在它们上使用双引号并完全删除 addslashes

      echo '<span class="song" id="'.$file.'" onClick="playSong("'.$file.'");'>'.$file.'</span><br />';
      

      或者您可以使用preg_replace 而不是addslashes$file 字符串中的所有单引号更改为它的ASCII HTML 实体(&amp;#39;):

      echo "<span class='song' id='".$file."' onClick='playSong(\"".preg_replace('/\'/', '&#39;', $file)."\");'>".$file."</span><br />";
      

      【讨论】:

        【解决方案4】:

        不要使用addslashes,而是使用htmlentities,这也避免了很多不相关的麻烦,例如当您的歌曲标题不是英文时会出现不同的编码。

        echo '<span class="song" id="'.$file.'" onClick="playSong(\''.htmlentities($file, ENT_QUOTES, 'UTF-8').'\');">'.htmlentities($file, ENT_QUOTES, 'UTF-8').'</span><br />';
        

        【讨论】:

          【解决方案5】:

          正如How to escape only single quotes? 中提到的,使用 json_encode(),这是注入安全的:

          $my_str = json_encode("using single quote here: '",JSON_HEX_APOS);
          
          echo $my_str;
          

          这会给你输出:

          using single quote here: \u0027
          

          它的作用与 JavaScript 中的 \' 相同。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-04-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多