php krc歌词解析
效果图:
代码如下:
function krc2lrc($krc_content, $glue = "n") {
$enKey = array(64, 71, 97, 119, 94, 50, 116, 71, 81, 54, 49, 45, 206, 210, 110, 105);
$krc_content = substr($krc_content, 4);
$len = strlen($krc_content);
$krc_compress = '';
for ($k = 0; $k < $len; $k++) {
$krc_compress .= chr(ord($krc_content[$k]) ^ $enKey[$k % 16]);
}
$krc_text = gzuncompress($krc_compress);
return $krc_text;
preg_match_all("/[(ar|ti|offset):(.+)]/", $krc_text, $matches);
$lrc_text = implode($glue, $matches[0]) . $glue;
preg_match_all("/[(d{0,8}),(d{0,8})](.+)/", $krc_text, $matches);
unset($krc_text, $krc_compress, $krc_content, $enKey);
if (!empty($matches[1])) {
foreach($matches[1] as $k => $v) {
list($seconds, $millis) = explode('.', $v / 1000);
$lrc_time =
date('i:s', 1388534400 + $seconds) . '.' . substr($millis, 0, 2);
$lrc_cont = preg_replace("/<d{1,8},d{1,8},d{1,8}>/", '', $matches[3][$k]);
$lrc_text .= "[{$lrc_time}]{$lrc_cont}{$glue}";
}
}
return $lrc_text;
}
简单的示例:
$content = file_get_contents("1.krc");
echo krc2lrc($content,'<br/>');
黑蜘蛛