带有空格的file_get_contents

时间:2012-03-07 06:53:25

标签: php file-get-contents spaces

我有一个问题,即使我将空格替换为%20并将此内容作为终极网址,浏览器将“%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 =红辣椒时,它也将空格转换为%20,但如果浏览器似乎将%'转换为%25。

有人能帮助我吗?

2 个答案:

答案 0 :(得分:13)

$song = $_GET['song']);

$url = "http://www.lyrdb.com/lookup.php?for=fullt&q=";

echo "list url is " . htmlentities($url . $song) . "<hr>";

$content = file_get_contents($url . urlencode($song));

echo $content;

答案 1 :(得分:0)

$data = json_encode($_POST);

$url = "http://www.index.com?data=";

echo file_get_contents($url . urlencode($data));