2011-12-29 38 views
13

我有一個mp3類爲我的網站閱讀mp3。 (我無法安裝該模塊,因爲它的共享主機)。我上傳了mp3,然後系統使用我的課程讀取了它,然後在我的mysql中插入了文件名和位置,以及基本標籤(藝術家,歌曲名稱,專輯)。 MySQL的插入是好的,但我有問題,我插入的字符串。php mp3字符錯誤

這裏是我的代碼:http://pastebin.com/fXsm0c3T

<?php 



class Id3 { 



    private $tags = array(

     'TALB' => 'album', 'TCON' => 'genre', 'TENC' => 'encoder', 

    'TIT2' => 'title', 'TPE1' => 'artist', 'TPE2' => 'ensemble', 'TYER' => 'year', 'TCOM' => 'composer', 

     'TCOP' => 'copyright', 'TRCK' => 'track', 'WXXX' => 'url', 

     'COMM' => 'comment' 

    ); 



    private $genre = array(

     0 => 'Blues', 1 => 'Classic Rock', 2 => 'Country', 3 => 'Dance', 4 => 'Disco', 5 => 'Funk', 6 => 'Grunge', 7 => 'Hip-Hop', 8 => 'Jazz', 9 => 'Metal', 10 => 'New Age', 11 => 'Oldies', 12 => 'Other', 

    13 => 'Pop', 14 => 'R&B', 15 => 'Rap', 16 => 'Reggae', 17 => 'Rock', 18 => 'Techno', 19 => 'Industrial', 20 => 'Alternative', 21 => 'Ska', 22 => 'Death Metal', 23 => 'Pranks', 24 => 'Soundtrack', 25 => 'Euro-Techno', 26 => 'Ambient', 27 => 'Trip-Hop', 28 => 'Vocal', 

     29 => 'Jazz+Funk', 30 => 'Fusion', 31 => 'Trance', 32 => 'Classical', 33 => 'Instrumental', 34 => 'Acid', 35 => 'House', 

     36 => 'Game', 37 => 'Sound Clip', 38 => 'Gospel', 39 => 'Noise', 40 => 'Alternative Rock', 41 => 'Bass', 42 => 'Soul', 43 => 'Punk', 44 => 'Space', 45 => 'Meditative', 46 => 'Instrumental Pop', 47 => 'Instrumental Rock', 48 => 'Ethnic', 

      49 => 'Gothic', 50 => 'Darkwave', 51 => 'Techno-Industrial', 52 => 'Electronic', 53 => 'Pop-Folk', 54 => 'Eurodance', 55 => 'Dream', 56 => 'Southern Rock', 57 => 'Comedy', 58 => 'Cult', 59 => 'Gangsta', 60 => 'Top 40', 61 => 'Christian Rap', 62 => 'Pop/Funk', 63 => 'Jungle', 64 => 'Native US', 65 => 'Cabaret', 66 => 'New Wave', 67 => 'Psychadelic', 68 => 'Rave', 69 => 'Showtunes', 70 => 'Trailer', 71 => 'Lo-Fi', 72 => 'Tribal', 73 => 'Acid Punk', 74 => 'Acid Jazz', 75 => 'Polka', 76 => 'Retro', 77 => 'Musical', 78 => 'Rock & Roll', 79 => 'Hard Rock', 80 => 'Folk', 81 => 'Folk-Rock', 82 => 'National Folk', 83 => 'Swing', 84 => 'Fast Fusion', 85 => 'Bebob', 86 => 'Latin', 

     87 => 'Revival', 88 => 'Celtic', 89 => 'Bluegrass', 90 => 'Avantgarde', 91 => 'Gothic Rock', 92 => 'Progressive Rock', 93 => 'Psychedelic Rock', 94 => 'Symphonic Rock', 95 => 'Slow Rock', 96 => 'Big Band', 97 => 'Chorus', 98 => 'Easy Listening', 99 => 'Acoustic', 

      100 => 'Humour', 101 => 'Speech', 102 => 'Chanson', 103 => 'Opera', 104 => 'Chamber Music', 105 => 'Sonata', 106 => 'Symphony', 107 => 'Booty Bass', 108 => 'Primus', 109 => 'Porn Groove', 110 => 'Satire', 111 => 'Slow Jam', 112 => 'Club', 

    113 => 'Tango', 114 => 'Samba', 115 => 'Folklore', 116 => 'Ballad', 117 => 'Power Ballad', 118 => 'Rhytmic Soul', 119 => 'Freestyle', 120 => 'Duet', 121 => 'Punk Rock', 122 => 'Drum Solo', 123 => 'Acapella', 124 => 'Euro-House', 

     125 => 'Dance Hall', 126 => 'Goa', 127 => 'Drum & Bass', 128 => 'Club-House', 129 => 'Hardcore', 130 => 'Terror', 131 => 'Indie', 132 => 'BritPop', 133 => 'Negerpunk', 134 => 'Polsk Punk', 135 => 'Beat', 136 => 'Christian Gangsta Rap', 137 => 'Heavy Metal', 138 => 'Black Metal', 139 => 'Crossover', 140 => 'Contemporary Christian', 141 => 'Christian Rock', 142 => 'Merengue', 143 => 'Salsa', 144 => 'Trash Metal', 145 => 'Anime', 146 => 'Jpop', 147 => 'Synthpop' 

     ); 



    public function __construct() { 

     $this->info = ''; 

    } 



    private function getId3() { 

    $handle = fopen($this->file, 'r'); 

      $head = fread($handle,10); 

      $head = unpack("a3signature/c1version_major/c1version_minor/c1flags/Nsize", $head); 

     $result = array(); 

    for ($i = 0; $i<5; $i++){ 

      $tag = trim(fread($handle, 6)); 

      if (!isset($this->tags[$tag])) continue;   

     $size = fread($handle, 2); 

      $size = unpack('n', $size); $size = $size[1]+2; 

     $value = fread($handle, $size); 

     $this->info[$this->tags[$tag]] = $value; 

     }  fclose($handle); 

    } 



    public function load($file) { 

     $this->file = $file; $this->getId3(); } 

} 





$id3 = new Id3(); 

    $id3->load('mp3.mp3'); 

print_R($id3->info); 

現在,當我讀到的值,我得到這樣的:

Array 
(
    [genre] => ��Rock 
    [title] => ��Walk 
    [artist] => ��Foo Fighters 
    [album] => ��Wasting Light 
) 

什麼是錯我的代碼?

+3

什麼奇怪的字符? – 2011-12-29 02:10:02

+0

可能你正在使用錯誤的算法。應該有你的問題的現有代碼,只是使用一些工作,可能從這裏開始:http://php.net/manual/en/ref.id3.php – hakre 2011-12-29 02:16:40

+3

就像我說的在我的問題,這些id3不可用和我不能添加他們 – 2011-12-29 02:21:52

回答

17

根據您的代碼,$值需要修剪或轉換爲UTF(我更喜歡轉換爲UTF)。 我添加的功能,將檢查哪些UTF是MP3和正確分析:

$value = $this->filter($value, $tag); // convert to UTF or else that is why you have weird chars 

這裏有一個readable code

<?php 

class Id3 { 

    private $tags = array(
    'TALB' => 'album', 
    'TCON' => 'genre', 
    'TENC' => 'encoder', 
    'TIT2' => 'title', 
    'TPE1' => 'artist', 
    'TPE2' => 'ensemble', 
    'TYER' => 'year', 
    'TCOM' => 'composer', 
    'TCOP' => 'copyright', 
    'TRCK' => 'track', 
    'WXXX' => 'url', 
    'COMM' => 'comment' 
); 

    private $genre = array(
    0 => 'Blues', 
    1 => 'Classic Rock', 
    2 => 'Country', 
    3 => 'Dance', 
    4 => 'Disco', 
    5 => 'Funk', 
    6 => 'Grunge', 
    7 => 'Hip-Hop', 
    8 => 'Jazz', 
    9 => 'Metal', 
    10 => 'New Age', 
    11 => 'Oldies', 
    12 => 'Other', 
    13 => 'Pop', 
    14 => 'R&B', 
    15 => 'Rap', 
    16 => 'Reggae', 
    17 => 'Rock', 
    18 => 'Techno', 
    19 => 'Industrial', 
    20 => 'Alternative', 
    21 => 'Ska', 
    22 => 'Death Metal', 
    23 => 'Pranks', 
    24 => 'Soundtrack', 
    25 => 'Euro-Techno', 
    26 => 'Ambient', 
    27 => 'Trip-Hop', 
    28 => 'Vocal', 
    29 => 'Jazz+Funk', 
    30 => 'Fusion', 
    31 => 'Trance', 
    32 => 'Classical', 
    33 => 'Instrumental', 
    34 => 'Acid', 
    35 => 'House', 
    36 => 'Game', 
    37 => 'Sound Clip', 
    38 => 'Gospel', 
    39 => 'Noise', 
    40 => 'Alternative Rock', 
    41 => 'Bass', 
    42 => 'Soul', 
    43 => 'Punk', 
    44 => 'Space', 
    45 => 'Meditative', 
    46 => 'Instrumental Pop', 
    47 => 'Instrumental Rock', 
    48 => 'Ethnic', 
    49 => 'Gothic', 
    50 => 'Darkwave', 
    51 => 'Techno-Industrial', 
    52 => 'Electronic', 
    53 => 'Pop-Folk', 
    54 => 'Eurodance', 
    55 => 'Dream', 
    56 => 'Southern Rock', 
    57 => 'Comedy', 
    58 => 'Cult', 
    59 => 'Gangsta', 
    60 => 'Top 40', 
    61 => 'Christian Rap', 
    62 => 'Pop/Funk', 
    63 => 'Jungle', 
    64 => 'Native US', 
    65 => 'Cabaret', 
    66 => 'New Wave', 
    67 => 'Psychadelic', 
    68 => 'Rave', 
    69 => 'Showtunes', 
    70 => 'Trailer', 
    71 => 'Lo-Fi', 
    72 => 'Tribal', 
    73 => 'Acid Punk', 
    74 => 'Acid Jazz', 
    75 => 'Polka', 
    76 => 'Retro', 
    77 => 'Musical', 
    78 => 'Rock & Roll', 
    79 => 'Hard Rock', 
    80 => 'Folk', 
    81 => 'Folk-Rock', 
    82 => 'National Folk', 
    83 => 'Swing', 
    84 => 'Fast Fusion', 
    85 => 'Bebob', 
    86 => 'Latin', 
    87 => 'Revival', 
    88 => 'Celtic', 
    89 => 'Bluegrass', 
    90 => 'Avantgarde', 
    91 => 'Gothic Rock', 
    92 => 'Progressive Rock', 
    93 => 'Psychedelic Rock', 
    94 => 'Symphonic Rock', 
    95 => 'Slow Rock', 
    96 => 'Big Band', 
    97 => 'Chorus', 
    98 => 'Easy Listening', 
    99 => 'Acoustic', 
    100 => 'Humour', 
    101 => 'Speech', 
    102 => 'Chanson', 
    103 => 'Opera', 
    104 => 'Chamber Music', 
    105 => 'Sonata', 
    106 => 'Symphony', 
    107 => 'Booty Bass', 
    108 => 'Primus', 
    109 => 'Porn Groove', 
    110 => 'Satire', 
    111 => 'Slow Jam', 
    112 => 'Club', 
    113 => 'Tango', 
    114 => 'Samba', 
    115 => 'Folklore', 
    116 => 'Ballad', 
    117 => 'Power Ballad', 
    118 => 'Rhytmic Soul', 
    119 => 'Freestyle', 
    120 => 'Duet', 
    121 => 'Punk Rock', 
    122 => 'Drum Solo', 
    123 => 'Acapella', 
    124 => 'Euro-House', 
    125 => 'Dance Hall', 
    126 => 'Goa', 
    127 => 'Drum & Bass', 
    128 => 'Club-House', 
    129 => 'Hardcore', 
    130 => 'Terror', 
    131 => 'Indie', 
    132 => 'BritPop', 
    133 => 'Negerpunk', 
    134 => 'Polsk Punk', 
    135 => 'Beat', 
    136 => 'Christian Gangsta Rap', 
    137 => 'Heavy Metal', 
    138 => 'Black Metal', 
    139 => 'Crossover', 
    140 => 'Contemporary Christian', 
    141 => 'Christian Rock', 
    142 => 'Merengue', 
    143 => 'Salsa', 
    144 => 'Trash Metal', 
    145 => 'Anime', 
    146 => 'Jpop', 
    147 => 'Synthpop' 
); 
    private $file; 
    private $info; 

    public function __construct() { 
    $this->info = array(
     'genre' => 'unknow', 
     'title' => 'unknow', 
     'artist' => 'unknow', 
     'album' => 'unknow', 
    ); 
    } 

    private function filter($tag, $type) { 
    if ($type == 'COMM') { 
     $tag = substr($tag, 0, 3) . substr($tag, 10); 
    } 
    if(ord($tag[2]) == 0) { 
     return iconv('UTF-8', 'ISO-8859-1', substr($tag, 3)); 
    } 
    elseif(ord($tag[2]) == 1) { 
     return iconv('UTF-16LE', 'UTF-8', substr($tag, 5)); 
    } 
    elseif(ord($tag[2]) == 2) { 
     return iconv('UTF-16BE', 'UTF-8', substr($tag, 5)); 
    } 
    elseif(ord($tag[2]) == 3) { 
     return substr($tag, 3); 
    } 
    return false; 
    } 

    private function getId3() { 
    $handle = fopen($this->file, 'rb'); 
    $head = fread($handle, 10); 
    $head = unpack("a3signature/c1version_major/c1version_minor/c1flags/Nsize", $head); 
    if (!$head['signature'] == 'ID3') { 
     fclose($handle); 
     return false; 
    } 
    $result = array(); 
    for ($i = 0; $i < 22; $i++) { //22 most popular tags, you had 5 therefore only the first five. 
     $tag = rtrim(fread($handle, 6)); 
     if (!isset($this->tags[$tag])) { 
     continue; 
     } 
     $size = fread($handle, 2); 
     $size = unpack('n', $size); 
     $size = $size[1]+2; 
     $value = fread($handle, $size); 
     $value = $this->filter($value, $tag); // convert to UTF or else that is why you have weird chars 
     $this->info[$this->tags[$tag]] = $value; 
    } 
    fclose($handle); 
    } 

    public function load($file) { 
    $this->file = $file; 
    $this->getId3(); 
    } 

    public function getInfo() { 
    return $this->info; 
    } 

} 

$id3 = new Id3(); 
$id3->load('mp3.mp3'); 
print_R($id3->getInfo()); 

要獲取更多有關標籤,讀取這個wiki文章:http://en.wikipedia.org/wiki/ID3

+1

這個作品謝謝 – 2011-12-29 02:27:07

+0

不客氣 – 2011-12-29 02:28:12