Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6996257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:07:58+00:00 2026-05-27T20:07:58+00:00

i have a mp3 class to read mp3s for my site. (i cannot install

  • 0

i have a mp3 class to read mp3s for my site. (i cannot install the module since its a shared hosting). i upload the mp3 and then the system read it using my class and insert into my mysql the file name and location, and the basic tag (artist, song name, album). the mysql insert is ok but i have problem with the strings i am inserting.

here’s my code: 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);

now, when i read the value i get this:

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

what is wrong with my code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:07:59+00:00Added an answer on May 27, 2026 at 8:07 pm

    based on your code, the $value needs to be trim or converted to UTF (I prefer converting to UTF).
    I added function that will check which UTF is the mp3 and parse it correctly:

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

    Here’s a 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());
    

    To get more information about tags, read this wiki article: http://en.wikipedia.org/wiki/ID3

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an mp3 file in my site. I want to output it as
I've read through the logging module documentation and whilst I may have missed something
I have the following error but cannot workout the problem, i am READING MP3
I have mp3 catalog portal and I want to customize my urls. I used
I have an mp3 player that sends MCI commands to play pause/ff/rw/stop the audio
i want to have an MP3 constantly playing and looping in an HTML page
I have a bunch of MP3 metadata in CouchDB. I want to return every
I have found a few libraries to edit MP3 tags (UltraID3Lib is great) but
I have an enum public enum FileExtentions { mp3, mpeg } And I have
I have a small utility that I use to download an MP3 file from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.