So, I'm creating a script that sometimes deals with artist and song names, and as you probably know, some artists/bands and songs have apostrophes in them. My problem here is that I'm trying to link to these bands/artists and songs, and the apostrophes in their names interferes with my link, and closes it before the full link is out.
Anyone know how I could fix this?
I tried htmlspecialcharacters()
but I guess I don't know how to use it correctly cause it doesn't parse the HTML if I use it.
This occurs when I use htmlspecialcharacters
.
Although it should look like this
So yeah, any ideas?
<?php
// Made by cxdy on GitHub
// Free to use, keep the credits intact please
$uname = "YOUR USERNAME"; // Your Last.FM username
$key = "YOUR API KEY"; // Your Last.FM API Key
$json = file_get_contents("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=" . $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 '<a href="http://last.fm/user/' . $uname . '/">';
echo "I'm currently listening to</a> <a href='http://last.fm/artist/" . str_replace(" ","+",$artist) . "/_/" . str_replace(" ","+",$currenttrack) . "'>" . $currenttrack . "</a>"; // Linking your song
echo " by <a href='http://last.fm/artist/" . str_replace(" ","+",$artist) . "'>" . $artist . "</a>"; // Links your artist, and shows what you're playing, when you play it.
}
?>
also please leave me alone about not using json_decode()
haha
EDIT
Also! I don't think I made my error clear.
Basically, imagine the song "I'm Lost Without You" by blink-182
If this song started playing, you'd think that the script would link them to that song, correct? It's supposed to link to http://last.fm/artist/blink-182/_/Im+Lost+Without+You
Well, the thing is, it doesn't. It links to http://last.fm/artist/blink-182/_/I
It gets to that apostrophe and closes itself off. So I also need to know how to turn apostrophes into nothing. Basically eliminate them. If I'm correct, str_replace
should work?