我遇到了一个问题,即使我将空格替换为%20并且获得了这个内容,但最终浏览器获取的url会将“%20”转换为“%2520”。
以下是我的代码,请问有什么建议可以解决这个问题吗?它看起来很简单,但我卡住了:/
<?php
//$_GET['song'] will contain a song name with spaces
$song = str_replace(array("%20", "&", "?" , "/"), array(" ", "", "", ""), $_GET['song']);
// I use this to check how the GET 'song' looks after the str_replace
$list = "http://www.lyrdb.com/lookup.php?q=" . $song . "&for=fullt";
echo "list url is " . $list . "<hr>";
$content = file_get_contents("http://www.lyrdb.com/lookup.php?q=" . str_replace(" ", "%20", $song) . "&for=fullt");
echo $content;
?>
如果您前往http://webservices.lyrdb.com/lookup.php?q=red%20hot%20chili%20peppers&for=fullt,结果将输出歌词代码列表。
当我访问我的网站/?song=red hot chili peppers时,它也会将空格转换为%20,但似乎浏览器将%的转换为%25。
有人能帮我解决吗?
htmlentities
和类似的函数。这有助于防止XSS攻击。urlencode
用于将URL的查询参数转换为通过GET方法传输数据的RFC标准。 - Cheery