8

I have an issue where even if I replace the spaces to %20 and get this content the ultimate url the browser gets turns the "%20" into "%2520"

Here's my code, any suggestions to get this to work? it seems easy but I'm stuck :/

<?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;
?>

if you go to http://webservices.lyrdb.com/lookup.php?q=red%20hot%20chili%20peppers&for=fullt The result should output a list of lyric codes.

When i go to my website /?song=red hot chili peppers , it too converts spaces to %20 's but if it seems the browser converts the %'s to %25.

Can someone help me out?

leppie
  • 115,091
  • 17
  • 196
  • 297
d-_-b
  • 21,536
  • 40
  • 150
  • 256

2 Answers2

17
$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;
Cheery
  • 16,063
  • 42
  • 57
  • THANK YOU Cheery !!!! (there's a ) after the first line, but other than that THANKS! When I tried using urlencode and htmlentities together but that caused problems. I can't thank you enough!! – d-_-b Mar 07 '12 at 07:02
  • 1
    @LearningDaily `htmlentities` and analogous functions should be used when you want to output something into the page with the data received from the user. it helps to prevent XSS attacks. `urlencode` is needed to convert URL's query parameters to the RFC standard of data transfer through the GET method. – Cheery Mar 07 '12 at 07:08
0
$data = json_encode($_POST);

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

echo file_get_contents($url . urlencode($data));
Ali Zain
  • 1
  • 2