1

我使用的是Kejun's Blog中的代码。

我想解析一个 .lrc (基本上是一个歌词文件),以便获取时间变量以及字符串(读取歌词)。我尝试了这段代码,但似乎无法获得输出。

   <html>
<head> 
<script src="jquery-1.7.1.js"></script>
<script> 
$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: "a.txt",
        dataType: "text",
        success: function (data) {
            parseLyric(data);
        }
    });
});

var _current_lyric = new Array();

function convertLRCLyric(inf) {
    inf += "n";
    var lyric = inf.match(/([(d{2}:d{2}(.d{1,2}){0,1})]){1,}W*n|([(d{2}:d{2}:d{2}(.d{1,2}){0,1})]){1,}W*n/ig);
    var l_s = '',
        l_tt, l_ww, l_i, l_ii;
    if (!lyric || !lyric.length) {
        return;
    }

    for (l_i = 0; l_i < lyric.length; l_i++) {
        l_tt = lyric[l_i].match(/([d{2}:d{2}(.d{1,2}){0,1}])|([d{2}:d{2}:d{2}(.d{1,2}){0,1}])/ig);
        l_ww = lyric[l_i].replace(/[S+]/ig, '').replace(/n{1,}/ig, '');
        for (l_ii = 0; l_ii < l_tt.length; l_ii++) {
            l_tt[l_ii] = l_tt[l_ii].replace(/[/,'').replace(/]/, '');
            if (l_tt[l_ii].search(/d{2}:d{2}:d{2}.d{2}/g) >= 0) {
                _current_lyric[l_tt[l_ii].substring(0, l_tt[l_ii].length - 1)] = l_ww;
            } else if (l_tt[l_ii].search(/d{2}:d{2}:d{2}.d{1}/g) >= 0) {
                _current_lyric[l_tt[l_ii]] = l_ww;
            } else if (l_tt[l_ii].search(/d{2}:d{2}:d{2}/g) >= 0) {
                _current_lyric[l_tt[l_ii] + ".0"] = l_ww;
            } else if (l_tt[l_ii].search(/d{2}:d{2}.d{2}/g) >= 0) {
                _current_lyric["00:" + l_tt[l_ii].substring(0, l_tt[l_ii].length - 1)] = l_ww;
            } else if (l_tt[l_ii].search(/d{2}:d{2}.d{1}/g) >= 0) {
                _current_lyric["00:" + l_tt[l_ii]] = l_ww;
            } else if (l_tt[l_ii].search(/d{2}:d{2}/g) >= 0) {
                _current_lyric["00:" + l_tt[l_ii] + ".0"] = l_ww;
            }
        }
    }
}

function parseLyric(allText) {
    _current_lyric = [];
    convertLRCLyric(allText);
    var ly = "";
    for (var time in _current_lyric) {
        ly += time + "--" + _current_lyric[time] + "n";
    }
    alert(ly);
}
</script>
</head>
<body>

</body>
</html>

但我一直收到空白警报。任何帮助都会很棒。提前致谢 。

4

2 回答 2

2

回答 :

好的,所以我构建了自己的解析器,这是代码

var contents = " " ;
function readMultipleFiles(evt) {
    //Retrieve all the files from the FileList object
    var files = evt.target.files;

    if (files) {
        for (var i = 0, f; f = files[i]; i++) {
            var r = new FileReader();
            r.onload = (function (f) {
                return function (e) {
                    contents = e.target.result;
                   processData(contents);
                };
            })(f);
            r.readAsText(f);
        }
    } else {
        alert("Failed to load files");
    }
}
document.getElementById('fileinput').addEventListener('change', readMultipleFiles, false);



var allTextLines = " ";
var lyrics = [];
var tim = [] ;
var line = " ";


// parsing the Lyrics 
function processData(allText) { // This will only divide with respect to new lines 
    allTextLines = allText.split(/\r\n|\n/);

     next();
   } 
 function next()
 {
for (i=0;i<allTextLines.length;i++)
{
if (allTextLines[i].search(/^(\[)(\d*)(:)(.*)(\])(.*)/i)>=0 )// any line without the prescribed format wont enter this loop 
{
    line = allTextLines[i].match(/^(\[)(\d*)(:)(.*)(\])(.*)/i);
        tim[i] = (parseInt(line[2])*60)+ parseInt(line[4]); // will give seconds 
        lyrics[i]= line[6] ;//will give lyrics 

 }
 }  

  } 
于 2012-07-27T14:57:30.043 回答
0

代码 php:带格式

public function get_lrc_song($song) {
    $lyrics_file = $song ['lyrics_file'];
    $json = curlClass::getInstance ( true )->fetchURL ( $lyrics_file );
    $content = explode ( "\n", $json );
    $regix = "$\][^>]+$";
    $result = "";
    foreach ( $content as $item ) {
        $isHas = preg_match ( $regix, $item, $data );
        $dt = str_replace ( "]", "", $data[0] );
        if ($dt != ""){
            $result .= $dt . "\n";
        }
    }
    echo $result;
}
于 2016-07-15T04:01:10.423 回答