2017-08-19 34 views
0

我有一個腳本來閱讀Shoutcast直播信息。Php在閱讀頁面時刪除html標籤

我正在獲取曲目的名稱並在spotify api上搜索以獲得封面藝術。

我的代碼:

$sc_url_ip = "streamip"; 
$sc_url_port = "streamport"; 

function getNowPlaying($sc_url_ip,$sc_url_port) 
{ 
    $open = fsockopen($sc_url_ip,$sc_url_port,$errno,$errstr,'.5'); 
    if ($open) { 
     fputs($open,"GET /7.html HTTP/1.1\nUser-Agent:Mozilla\n\n"); 
     stream_set_timeout($open,'1'); 
     $read = fread($open,200); 
     $text = explode(",",$read); 
     if($text[6] == '' || $text[6] == '</body></html>'){ 
      $msg = ' live stream offline'; } 
     else { 
      $msg = $text[6]; 
     } 
     $text = $msg; 
    } 
    else { 
     return false; 
    } 

    fclose($open); 
    return $text; 
} 

$current_song = getNowPlaying($sc_url_ip,$sc_url_port); 
echo $current_song; 

但隨着Spotify的API進行搜索時,我的曲目名稱看起來像這樣在源代碼:

Pink Floyd - Welcome to the Machine</body></html> 

因爲HTML標籤,Spotify的API,搜索結果爲零。查看搜索網址:

https://api.spotify.com/v1/search?query=Pink+Floyd+-+Welcome+to+the+Machine%3C%2Fbody%3E%3C%2Fhtml%3E%3B&type=track

我試圖用strip_tags,但我不能。 如何從$ current_song中刪除這些</body></html>標籤?

+0

你可以嘗試使用PHP的[strip_tags()](http://php.net/manual/en/function.strip-tags.php)。 –

+0

是的,我試過了,但我不知道strip_tags到哪個變量 – Solhan

+1

到包含曲目名稱的變量。將它添加到函數的返回中:'return strip_tags($ text);' –

回答

2

變量$current_song包含html標籤吧?然後去除它,試試這個:

$current_song = strip_tags(getNowPlaying($sc_url_ip,$sc_url_port)); 
+0

這就是它!有效。非常感謝:) – Solhan

+1

歡迎!很高興可以幫助。 :) – Jixone