1

I'm writing a Last.FM script that shows what song you're listening to, and I'd like to be able to link the artist to the song title and everything.

How would I go about doing this? I mean, I'm assuming that I'll use str_replace(), but how?

The way Last.FM formats their urls is like this;

http://last.fm/artist/This+Is+A+Band

So basically what I'm asking is, how would I take my $artist variable, and convert those spaces to pluses, but only for the part of the script that prints the song information.

Here's my code:

$uname = "USERNAME";
$key = "API KEY";
$json = file_get_contents("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks=" . $uname . "&api_key=" . $key . "&format=json"); // Gets your data
if (strpos($json, 'nowplaying') !== false) {
list($currentsonginfo, $crap) = explode('{"nowplaying":"true"}}', $json);
list($crap, $v2) = explode('{"recenttracks":{"track":[{"artist":{"#text":"', $currentsonginfo);
list($artist, $restinfo) = explode('","', $v2);
list($crap, $currenttrack) = explode('"name":"', $restinfo); }
$playing = $artist . ' - ' . $currentrack; // Checks if a song is playing
if ($playing == ' - ') {
    echo '<a href="http://last.fm/user/' . $uname . '/">';
    echo "I'm not listening to anything."; // Only if you're not scrobbling
    echo "</a>";
} else {
    echo '<marquee>';
    echo '<a href="http://last.fm/user/' . $uname . '/">';
    echo "I'm currently listening to " . $currenttrack . " by " . $artist; // It shows when you're playing music!
    echo "</a>";
    echo '</marquee>';
}
gparyani
  • 1,958
  • 3
  • 26
  • 39
Cody
  • 219
  • 2
  • 12

2 Answers2

0

PHP parse_url()

php's parse_url function will be very useful for this.

<?php
$url = 'http://last.fm/artist/This+Is+A+Band';

print_r(parse_url($url));

echo parse_url($url, PHP_URL_PATH); // will print an array of the different parts of the url you can access with flags

From there, it's just a matter of stripping the stuff you don't want by exploding the components of the path returned, getting the last item in your exploded array, and using str_replace with the params being in the order search, replace, haystack

<?php
$url = 'http://last.fm/artist/This+Is+A+Band';

$apiPath = parse_url($url, PHP_URL_PATH));

$parts = explode($apiPath, '/'); // get an array of the path components so you can access the artist

$artist = end($parts);  // moves array pointer to last structure

$artist = str_replace(' ','+',$artist); // turn your + into spaces
Brian Vanderbusch
  • 3,313
  • 5
  • 31
  • 43
0

A url should never have spaces, so you can always convert all the spaces to +. Hence, you can just use str_replace(" ", "+", $artist).

Also, you can parse the json by using json_decode instead of exploding the string.

marrock
  • 301
  • 1
  • 2
  • 12
  • Oh, okay. Cool, thank you. Any chance you could tell me where in the world I should put the `str_replace()`? I've never used it before so I don't know how it works with URLs. – Cody Aug 09 '14 at 05:41
  • Replace $artist by str_replace(" ", "+", $artist): echo "I'm currently listening to " . $currenttrack . " by " . str_replace(" ", "+", $artist); // It shows when you're playing music! – marrock Aug 09 '14 at 05:42
  • Your first parameter is search, and the second is replace, so this answer is backwards, it should be `str_replace("+"," ",$artist);` – Brian Vanderbusch Aug 09 '14 at 05:43
  • Actually, Brian, you were wrong. It's how marrock said it was. – Cody Aug 09 '14 at 05:54
  • yeah... I thought you were switching the other way around (pluses into spaces. Sorry about that. – Brian Vanderbusch Aug 10 '14 at 08:12
  • Oh, yeah, no problem @BrianVanderbusch. – Cody Aug 11 '14 at 00:43
  • Also, whoever downvoted @marrock's answer, what for? His answer was exactly what I needed. – Cody Aug 11 '14 at 00:43