gpt4 book ai didi

javascript - PHP 试图转义单引号

转载 作者:行者123 更新时间:2023-12-02 17:56:57 25 4
gpt4 key购买 nike

我正在创建一个音乐播放器。但 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>

最佳答案

只需更改引号:

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

PS:我也不太确定您是否需要 addslashes

关于javascript - PHP 试图转义单引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20885501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com