jvargasconde

M3U PARSER

Mar 23rd, 2017
398
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. ob_start();
  4. error_reporting(E_ALL);
  5.  
  6. class m3uParser {
  7.  
  8.   private $m3uFile;
  9.   private $m3uFile_SongLengths;
  10.   private $m3uFile_SongTitles;
  11.   private $m3uFile_SongLocations;
  12.  
  13.   public function __construct($m3uFile) {
  14.     /**
  15.     * @desc Load the file into an array
  16.     **/
  17.     if(file_exists($m3uFile))
  18.       $this -> m3uFile = file($m3uFile);
  19.     else
  20.       die("Unable to locate '$m3uFile'");
  21.  
  22.     /**
  23.     * @desc "Loosely" check that the file is an m3u file
  24.     **/
  25.     if(strtoupper(trim($this -> m3uFile[0])) != "#EXTM3U")
  26.       die("The file specified {$this -> m3uFileLocation} is not a valid M3U playlist.");
  27.  
  28.     /**
  29.     * @desc Remove extra empty lines
  30.     */
  31.     $buffer = array();
  32.     foreach($this -> m3uFile as $line) {
  33.       if($line != "\n" || $line != "\r" || $line != "\r\n" || $line != "\n\r")
  34.         $buffer[] = $line;
  35.     }
  36.     $this -> m3uFile = $buffer;
  37.  
  38.     /**
  39.     * @desc Shift the first line "#EXTM3U" off the array
  40.     **/
  41.     array_shift($this -> m3uFile);
  42.  
  43.     /**
  44.     * @desc Start parsing the m3u file
  45.     */
  46.     $this -> _init();
  47.   }
  48.  
  49.   /**
  50.   * @desc Hopefully free some memory (though not yet proven to work as thought)
  51.   */
  52.   public function __destruct() {
  53.     unset($this);
  54.   }
  55.  
  56.   /**
  57.   * @desc Initiate each array storing the Song Lengths, Titles and Locations
  58.   */
  59.   private function _init() {
  60.     foreach($this -> m3uFile as $key => $line) {
  61.       if(strtoupper(substr($line, 0, 8)) == "#EXTINF:") {
  62.         $line = substr_replace($line, "", 0, 8);
  63.         $line = explode(",", $line, 2);
  64.  
  65.         $this -> m3uFile_SongLengths[]   = $line[0];
  66.         $this -> m3uFile_SongTitles[]    = $line[1];
  67.         $this -> m3uFile_SongLocations[] = $this -> m3uFile[$key + 1];
  68.       }
  69.     }
  70.   }
  71.  
  72.   /**
  73.   * @desc Single or Multi case[in]sensitive searching
  74.   * @return array Returns array such as ["search string"] => "result[s]"
  75.   */
  76.   public function searchTitles($search, $caseSensitive = false) {
  77.     $results = array();
  78.  
  79.     if(is_array($search)) {
  80.       foreach($search as $terms) {
  81.         foreach($this -> m3uFile_SongTitles as $songTitle) {
  82.           $_search = $caseSensitive ? strstr($songTitle, $terms) : stristr($songTitle, $terms);
  83.  
  84.           if($_search)
  85.             $results[$terms][] = $songTitle;
  86.         }
  87.       }
  88.     } else {
  89.       foreach($this -> m3uFile_SongTitles as $songTitle) {
  90.         $_search = $caseSensitive ? strstr($songTitle, $search) : stristr($songTitle, $search);
  91.  
  92.         if($_search)
  93.           $results[] = $songTitle;
  94.       }
  95.     }
  96.  
  97.     return $results;
  98.   }
  99.  
  100.   /**
  101.   * @desc Single or Multi case[in]sensitive searching
  102.   * @return array Returns array such as ["search string"] => "result[s]"
  103.   */
  104.   public function searchLocations($search, $ignoreDirectorySeperator = true, $caseSensitive = false) {
  105.     $results = array();
  106.  
  107.     if(is_array($search)) {
  108.       foreach($search as $terms) {
  109.         foreach($this -> m3uFile_SongLocations as $songLocation) {
  110.           if($ignoreDirectorySeperator)
  111.             $_search = $caseSensitive ? strstr(str_replace(array("/", "\\"), "", $songLocation), $terms) : stristr(str_replace(array("/", "\\"), "", $songLocation), $terms);
  112.           else
  113.             $_search = $caseSensitive ? strstr($songLocation, $terms) : stristr($songLocation, $terms);
  114.  
  115.           if($_search)
  116.             $results[$terms][] = $songLocation;
  117.         }
  118.       }
  119.     } else {
  120.       foreach($this -> m3uFile_SongLocations as $songLocation) {
  121.         if($ignoreDirectorySeperator)
  122.           $_search = $caseSensitive ? strstr(str_replace(array("/", "\\"), "", $songLocation), $search) : stristr(str_replace(array("/", "\\"), "", $songLocation), $search);
  123.         else
  124.           $_search = $caseSensitive ? strstr($songLocation, $terms) : stristr($songLocation, $terms);
  125.  
  126.         if($_search)
  127.           $results[] = $songLocation;
  128.       }
  129.     }
  130.  
  131.     return $results;
  132.   }
  133.  
  134.   /**
  135.   * @desc Search song lengths by equal length, less than length, less than or equal to length, greater than length, greater than or equal to length or in between [start, end].
  136.   * @return array Returns array such as ["length"] => "title[s]"
  137.   */
  138.   public function searchLengths($type, $start, $end = null) {
  139.     $results = array();
  140.  
  141.     foreach($this -> m3uFile_SongLengths as $key => $length) {
  142.       switch($type) {
  143.         // Find lengths that equal to $start
  144.         case 0: {
  145.           if(!is_array($start)) {
  146.             if($length == $start)
  147.               $results[] = array($length => $this -> m3uFile_SongTitles[$key]);
  148.           } else {
  149.             foreach($start as $sLength) {
  150.               if($sLength == $length)
  151.                 $results[] = array($sLength => $this -> m3uFile_SongTitles[$key]);
  152.             }
  153.           }
  154.         } break;
  155.  
  156.         // Find lengths that are less than $start
  157.         case 1: {
  158.           if(!is_array($start)) {
  159.             if($start < $length)
  160.               $results[] = array($length => $this -> m3uFile_SongTitles[$key]);
  161.           } else {
  162.             foreach($start as $length) {
  163.               if($sLength < $sLength)
  164.                 $results[] = array($sLength => $this -> m3uFile_SongTitles[$key]);
  165.             }
  166.           }
  167.         } break;
  168.  
  169.         // Find lengths that are less than or equal to $start
  170.         case 2: {
  171.           if(!is_array($start)) {
  172.             if($start <= $length)
  173.               $results[] = array($length => $this -> m3uFile_SongTitles[$key]);
  174.           } else {
  175.             foreach($start as $sLength) {
  176.               if($sLength <= $length)
  177.                 $results[] = array($sLength => $this -> m3uFile_SongTitles[$key]);
  178.             }
  179.           }
  180.         } break;
  181.  
  182.         // Find lengths that are longer than $start
  183.         case 3: {
  184.           if(!is_array($start)) {
  185.             if($start > $length)
  186.               $results[] = array($length => $this -> m3uFile_SongTitles[$key]);
  187.           } else {
  188.             foreach($start as $sLength) {
  189.               if($sLength > $length)
  190.                 $results[] = array($sLength => $this -> m3uFile_SongTitles[$key]);
  191.             }
  192.           }
  193.         } break;
  194.  
  195.         // Find lengths that are longer or equal to $start
  196.         case 4: {
  197.           if(!is_array($start)) {
  198.             if($start >= $length)
  199.               $results[] = array($length => $this -> m3uFile_SongTitles[$key]);
  200.           } else {
  201.             foreach($start as $sLength) {
  202.               if($sLength >= $length)
  203.                 $results[] = array($sLength => $this -> m3uFile_SongTitles[$key]);
  204.             }
  205.           }
  206.         } break;
  207.  
  208.         // Find lengths between $start and $end
  209.         case 5: {
  210.           if(!is_array($start) && !is_array($end)) {
  211.             if($length >= $start && $length <= $end)
  212.               $results[] = array($length => $this -> m3uFile_SongTitles[$key]);
  213.           } else {
  214.             foreach($start as $sLength) {
  215.               if($sLength >= $start[$key] && $sLength <= $end[$key])
  216.                 $results[] = array($sLength => $this -> m3uFile_SongTitles[$key]);
  217.             }
  218.           }
  219.         } break;
  220.       }
  221.     }
  222.  
  223.     return $results;
  224.   }
  225.  
  226.   /**
  227.   * @desc Output the m3u in a human-readable format (includes table-output)
  228.   * @return string The buffer for output
  229.   */
  230.   public function prettyOutput($sortWhat = "songTitle", $sortDirection = "asc", $drawTable = false, $tableWidth = 700, $table_cellSpacing = 0, $table_cellPadding = 0, $table_tableBorder = 0, $table_params = null) {
  231.     $buffer = "";
  232.  
  233.     // Get statistics
  234.     $totalSongs    = number_format(count($this -> m3uFile_SongTitles));
  235.     $totalPlayTime = 0;
  236.  
  237.     foreach($this -> m3uFile_SongLengths as $length)
  238.       $totalPlayTime += $length;
  239.  
  240.     $totalPlayTime = $this -> formatPlayTime($totalPlayTime);
  241.  
  242.     // Output
  243.     if($drawTable) {
  244.      
  245.  
  246.       if($sortWhat == "songTitle") {
  247.         if($sortDirection == "asc")
  248.           $buffer .= "  <td align=\"center\" {$table_params}>[ASC] <u><strong>Nombre Canal</strong></u></td>\n";
  249.         else
  250.           $buffer .= "  <td align=\"center\" {$table_params}>[DESC] <u><strong>Nombre Canal</strong></u></td>\n";
  251.       } else {
  252.        
  253.       }
  254.  
  255.       if($sortWhat == "songLocation") {
  256.         if($sortDirection == "asc")
  257.           $buffer .= "  <td align=\"center\" {$table_params}>[ASC] <u><strong>Url Canal</strong></u></td>\n";
  258.         else
  259.           $buffer .= "  <td align=\"center\" {$table_params}>[DESC] <u><strong>Url Canal</strong></u></td>\n";
  260.       } else {
  261.        
  262.       }
  263.  
  264.      
  265.  
  266.       switch($sortWhat) {
  267.         // Sort by song title (using $sortDirection) -- this is the default sorting method
  268.         case "songTitle": {
  269.           $songTitles = $this -> m3uFile_SongTitles;
  270.           natcasesort($songTitles);
  271.  
  272.           if($sortDirection == "desc")
  273.             $songTitles = array_reverse($songTitles);
  274.  
  275.           foreach($songTitles as $key => $title) {
  276.             $title    = trim($title);
  277.             $location = trim($this -> m3uFile_SongLocations[$key]);
  278.             $length   = trim($this -> m3uFile_SongLengths[$key]);
  279.  
  280.            
  281.             $buffer .= "  <td {$table_params}>{$title}</td>\n";
  282.             $buffer .= "  <td {$table_params}>{$location}</td>\n";
  283.             //$buffer .= "  <td {$table_params}>{$length}</td>\n";
  284.      
  285.           }
  286.         } break;
  287.  
  288.         // Sort by song location (using $sortDirection)
  289.         case "songLocation": {
  290.           $songLocations = $this -> m3uFile_SongLocations;
  291.           natcasesort($songLocations);
  292.  
  293.           if($sortDirection == "desc")
  294.             $songLocations = array_reverse($songLocations);
  295.  
  296.           foreach($songLocations as $key => $location) {
  297.             $title    = trim($this -> m3uFile_SongTitles[$key]);
  298.             $location = trim($location);
  299.             $length   = trim($this -> m3uFile_SongLengths[$key]);
  300.  
  301.            
  302.             $buffer .= "  <td {$table_params}>{$title}</td>\n";
  303.             $buffer .= "  <td {$table_params}>{$location}</td>\n";
  304.            
  305.           }
  306.         } break;
  307.  
  308.         // Sort by song length (using $sortDirection)
  309.         case "songLength": {
  310.           $songLengths = $this -> m3uFile_SongLengths;
  311.           natsort($songLengths);
  312.  
  313.           if($sortDirection == "desc")
  314.             $songLengths = array_reverse($songLengths);
  315.  
  316.           foreach($songLengths as $key => $length) {
  317.             $title    = trim($this -> m3uFile_SongTitles[$key]);
  318.             $location = trim($this -> m3uFile_SongLocations[$key]);
  319.             $length   = trim($length);
  320.  
  321.            
  322.             $buffer .= "{$title}|";
  323.           $buffer .= "{$location}\n";
  324.            
  325.            
  326.           }
  327.         } break;
  328.       }
  329.  
  330.     } else {
  331.       foreach($this -> m3uFile_SongTitles as $key => $title) {
  332.         $location   = $this -> m3uFile_SongLocations[$key];
  333.         $length     = $this -> m3uFile_SongLengths[$key];
  334.         $buffer .= "Song Title: {$title} - Song Location: {$location} - Song Length: {$length} seconds\n<br />\n";
  335.       }
  336.  
  337.       $buffer .= "There are a total of {$totalSongs} with a combined play time of {$totalPlayTime}.";
  338.     }
  339.  
  340.     return $buffer;
  341.   }
  342.  
  343.   /**
  344.   * @desc Format a human-readable length time
  345.   * @return string Returns a formatted, human-readable play time length
  346.   */
  347.   public function formatPlayTime($seconds) {
  348.     $return = "";
  349.  
  350.     $hours = intval(intval($seconds) / 3600);
  351.     if($hours > 0)
  352.       $return .= "$hours hours, ";
  353.  
  354.     $minutes = (intval($seconds) / 60) % 60;
  355.     if($hours > 0 || $minutes > 0)
  356.       $return .= "$minutes minutes, and ";
  357.  
  358.     $seconds = intval($seconds) % 60;
  359.     $return .= "$seconds seconds";
  360.  
  361.     return $return;
  362.   }
  363.  
  364.   /**
  365.   * @desc Prints each array (Song Lengths, Song Titles, Song Locations)
  366.   */
  367.   public function debug() {
  368.     echo "<pre>";
  369.     print_r($this -> m3uFile_SongLengths);
  370.     print_r($this -> m3uFile_SongTitles);
  371.     print_r($this -> m3uFile_SongLocations);
  372.     echo "</pre>";
  373.   }
  374. }
  375.  
  376.  
  377. $m3uParser = new m3uParser("lista.m3u");
  378.  
  379. echo $m3uParser -> prettyOutput("songLength", "desc", true, "100%", 5, 5, 1, "center", "style=\"border: 1px solid #000;border-collapse: collapse;\"");
  380.  
  381. ?>
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×