Php str_替换为Last.fm URL

Php str_替换为Last.fm URL,php,str-replace,last.fm,Php,Str Replace,Last.fm,我正在写一个Last.FM脚本,显示您正在听的歌曲,我希望能够将艺术家链接到歌曲标题和所有内容 我该怎么做呢?我的意思是,我假设我将使用str\u replace(),但是如何使用呢 Last.FM格式化URL的方式如下: 所以基本上我要问的是,我如何获取$artist变量,并将这些空格转换为plus,但仅限于打印歌曲信息的脚本部分 这是我的密码: $uname = "USERNAME"; $key = "API KEY"; $json = file_get_contents("http://

我正在写一个Last.FM脚本,显示您正在听的歌曲,我希望能够将艺术家链接到歌曲标题和所有内容

我该怎么做呢?我的意思是,我假设我将使用
str\u replace()
,但是如何使用呢

Last.FM格式化URL的方式如下:

所以基本上我要问的是,我如何获取$artist变量,并将这些空格转换为plus,但仅限于打印歌曲信息的脚本部分

这是我的密码:

$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>';
}
$uname=“USERNAME”;
$key=“API密钥”;
$json=文件\u获取\u内容(“http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks=“$uname.”&api_key=“.$key.”&format=json”);//获取您的数据
if(strpos($json,'nowplaying')!==false){
list($currentsonginfo,$crap)=分解(“{”nowplaying:“true”}},$json);
list($crap,$v2)=分解('{“recenttracks”:{“track”:[{“artist”:{“#text”:“',$currentsonginfo);
列表($artist,$restinfo)=分解(“,”,$v2);
列表($crap,$currenttrack)=分解('name:',$restinfo);}
$playing=$artist.'-'.$currentrack;//检查是否正在播放歌曲
如果($playing='-')){
回声';
}否则{
回声';
回声';
回声';
}
PHP
parse\u url()
php在这方面非常有用

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

url不应该有空格,因此您可以始终将所有空格转换为+。因此,您可以使用str_replace(“,“+”,$artist)


另外,您可以使用json_decode而不是分解字符串来解析json。

哦,好的。很酷,谢谢。您有没有可能告诉我,我应该把
str_replace()
?我以前从未使用过它,所以我不知道它如何与URL一起工作。用str_replace(“,“+”,$artist)替换$artist:echo“我正在收听“$currenttrack.”by“.str_replace”(“,“+”,$artist);//它在播放音乐时显示!第一个参数是search,第二个参数是replace,所以这个答案是反向的,应该是
str_replace(“+”,“,$artist))好的,太好了!非常感谢你,@marrock!实际上,Brian,你错了。marrock就是这么说的。
<?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