douan7529 2014-01-02 14:51
浏览 26
已采纳

PHP试图逃脱单引号

I'm creating a music player. But the PHP just messes up filenames that have a singlequote in it. How can I fix this?

The first code is my current PHP. The second code is how i want the output to be.

// 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 />";
    }
}

At first I did HTML, But now I want to use PHP so i can just drag songs into a folder and they get automaticly added to the list.

This is how the HTML was, which is how I want the output from the PHP to be.

<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>
  • 写回答

5条回答 默认 最新

  • doutuo8800 2014-01-02 14:56
    关注

    Just change the quotes around:

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

    PS: I'm also not really sure, if you need the addslashes.

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

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向