2012-10-12 110 views
-1

我使用下面的代碼打印的MP2文件的ID3標籤,但輸出以這種形式問題在打印輸出

Array ([FileName] => 4.mp3 [TAG] => ID3 [Version] => 3.0 [Title] => &Bahut Khubsurat Ghazal (mr-Jatt.Com) [Album] => 1Loverz Choice (A Khubsurat Ghazal) (www.mzc.in) [Author] => DJ Badboy (mr-Jatt.Com) [Track] => (www.mzc.in)) 

快到了,但我想簡單的輸出這樣

文件名4 .mp3標題Bahut Khubsurat Ghazal(mr-Jatt.Com)專輯1Loverz Choice(A Khubsurat Ghazal)(www.mzc.in)作者DJ Badboy(mr-Jatt.Com)Track(www.mzc.in)

我的代碼是

print_r(tagReader("4.mp3")); 
// ------------------------------ 
function tagReader($file) { 
    $id3v23 = array("TIT2","TALB","TPE1","TRCK","TDRC","TLEN","USLT"); 
    $id3v22 = array("TT2","TAL","TP1","TRK","TYE","TLE","ULT"); 
    $fsize = filesize($file); 
    $fd = fopen($file, "r"); 
    $tag = fread($fd, $fsize); 
    $tmp = ""; 
    fclose($fd); 
    if (substr($tag, 0, 3) == "ID3") { 
     $result['FileName'] = $file; 
     $result['TAG'] = substr($tag, 0, 3); 
     $result['Version'] = hexdec(bin2hex(substr($tag, 3, 1))) . "." . hexdec(bin2hex(substr($tag, 4, 1))); 
    } 
    if ($result['Version'] == "4.0" || $result['Version'] == "3.0") { 
     for($i = 0; $i < count($id3v23); $i ++) { 
      if (strpos($tag, $id3v23[$i] . chr(0)) != FALSE) { 
       $pos = strpos($tag, $id3v23[$i] . chr(0)); 
       $len = hexdec(bin2hex(substr($tag, ($pos + 5), 3))); 
       $data = substr($tag, $pos, 9 + $len); 
       for($a = 0; $a < strlen($data); $a ++) { 
        $char = substr($data, $a, 1); 
        if ($char >= " " && $char <= "~") 
         $tmp .= $char; 
       } 
       if (substr($tmp, 0, 4) == "TIT2") 
        $result['Title'] = substr($tmp, 4); 
       if (substr($tmp, 0, 4) == "TALB") 
        $result['Album'] = substr($tmp, 4); 
       if (substr($tmp, 0, 4) == "TPE1") 
        $result['Author'] = substr($tmp, 4); 
       if (substr($tmp, 0, 4) == "TRCK") 
        $result['Track'] = substr($tmp, 4); 
       if (substr($tmp, 0, 4) == "TDRC") 
        $result['Year'] = substr($tmp, 4); 
       if (substr($tmp, 0, 4) == "TLEN") 
        $result['Lenght'] = substr($tmp, 4); 
       if (substr($tmp, 0, 4) == "USLT") 
        $result['Lyric'] = substr($tmp, 7); 
       $tmp = ""; 
      } 
     } 
    } 
    if ($result['Version'] == "2.0") { 
     for($i = 0; $i < count($id3v22); $i ++) { 
      if (strpos($tag, $id3v22[$i] . chr(0)) != FALSE) { 
       $pos = strpos($tag, $id3v22[$i] . chr(0)); 
       $len = hexdec(bin2hex(substr($tag, ($pos + 3), 3))); 
       $data = substr($tag, $pos, 6 + $len); 
       for($a = 0; $a < strlen($data); $a ++) { 
        $char = substr($data, $a, 1); 
        if ($char >= " " && $char <= "~") 
         $tmp .= $char; 
       } 
       if (substr($tmp, 0, 3) == "TT2") 
        $result['Title'] = substr($tmp, 3); 
       if (substr($tmp, 0, 3) == "TAL") 
        $result['Album'] = substr($tmp, 3); 
       if (substr($tmp, 0, 3) == "TP1") 
        $result['Author'] = substr($tmp, 3); 
       if (substr($tmp, 0, 3) == "TRK") 
        $result['Track'] = substr($tmp, 3); 
       if (substr($tmp, 0, 3) == "TYE") 
        $result['Year'] = substr($tmp, 3); 
       if (substr($tmp, 0, 3) == "TLE") 
        $result['Lenght'] = substr($tmp, 3); 
       if (substr($tmp, 0, 3) == "ULT") 
        $result['Lyric'] = substr($tmp, 6); 
       $tmp = ""; 
      } 
     } 
    } 
    return $result; 
} 

回答

0

您可以嘗試

foreach (tagReader("4.mp3") as $name => $value) { 
    printf("<p>%s:%s</p>", $name, $value); 
} 
+0

感謝它的工作 – payal

+0

@payal歡迎你.. .. :) – Baba

0

這是因爲你正在返回一個數組。如果你想打印出數組元素的內容,你將不得不遍歷數組並打印每個元素,而不是返回整個數組。

喜歡的東西:

foreach ($results as $key => $value) 
    echo $key . " " . $value; 
+0

可以請你建議如何實現這一 – payal

+0

@payal - 看到修改後的答案 – Jim