0

在此处输入图像描述

我使用这个 API 来抓取歌词: http: //www.chartlyrics.com/api.aspx

不幸的是,文本不是 html 缩进的。如何解析它以便在网络浏览器中我有正确的间距/缩进?

if ($_GET['get_lyrics'] == 1 AND $_GET['song_name'] AND $_GET['song_author']) {
    //via http://api.chartlyrics.com/
    $url = "http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=".$_GET['song_author']."&song=".$_GET['song_name'];
    $url = str_replace(" ", '%20', $url);

    //
    $header = array();
    $header[] = 'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
    $header[] = 'Cache-Control: max-age=0';
    $header[] = 'Connection: keep-alive';
    $header[] = 'Keep-Alive: 300';
    $header[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7';
    $header[] = 'Accept-Language: en-us,en;q=0.5';
    $header[] = 'Pragma: ';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729)');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_ENCODING, '');
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    $result = curl_exec($ch);
    curl_close ($ch);

    //
    if ($result) {
        $xml = new SimpleXMLElement($result);
        print_r(json_encode($xml->Lyric));
    }
}
4

2 回答 2

1

I am not sure what are you trying to do, but try nl2br($text) to add html line endings and then use CSS to style it the way you want, other option is to put it in <pre></pre> tags and style the element.

于 2013-09-03T09:28:01.610 回答
0

You could use a pre instead of a div.

Or parse the text :

  • Use a regex to get the different paragraphs and use p around them.
  • Or just replace "\n" in your text with br
  • use nl2br
于 2013-09-03T09:28:13.333 回答