Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // get the length of the array between file: and Id: ($mpdstatus) by getting the key for Id:
- // add 1 to the key as it begins at zero
- // then chunk the array using the value determined above
- // for each song, search the returned array for the fields Title: and Artist: (this is done by the array_search_partial function)
- // and this will return the keys
- // using the keys, then get the required fields ($songArtist = $song[$artist_key];)
- // this returns the full string (Title: Burnin’ Sky) so anything before the colon is stripped using the stripColon function
- //
- function makePlaylist($host,$port) {
- $opt = "playlistid";
- $mpdstatus = getStatus($opt,$host,$port);
- array_shift($mpdstatus);
- array_pop($mpdstatus);
- $endArray = array_search_partial($mpdstatus, "Id:");
- $endArray = abs($endArray + "1");
- $status=(array_chunk($mpdstatus,$endArray));
- array_pop($status);
- foreach ($status as $song) {
- $artist_key=array_search_partial($song, "Artist:"); // e.g. albumartist etc.
- $title_key=array_search_partial($song, "Title:");
- $songArtist = $song[$artist_key];
- $songTitle = $song[$title_key];
- $songArtist = stripColon($songArtist);
- $songTitle = stripColon($songTitle);
- echo "$songArtist - $songTitle<br />\n";
- }
- }
- function stripColon($opt) {
- if (!empty($opt)) {
- $state=explode(":",$opt);
- $opt=$state['1'];
- $opt=trim($opt);
- } else {
- $opt = "unknown";
- }
- return $opt;
- }
- function array_search_partial($arr, $keyword) {
- foreach($arr as $index => $string) {
- if (strpos($string, $keyword) !== FALSE)
- return $index;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement