Perfect seperator for Shoutcast 7.html file

Posted by Gerben Jacobs on April 8, 2012 in tech, php

To go along nicely with my previous Shoutcast tip, here is a trick to separate the data in the Shoutcast 7.html for displaying.

If you, like me, use the Shoutcast 7.html trick to get some quick stats about your stream broadcast but are getting some inconsistencies with parsing the comma-separated input you get, try this:

<?php $data = preg_split("/[,]+/", $string, 7); ?>

A possible outcome would be this:

Array (
[0] => 1
[1] => 1
[2] => 1
[3] => 1000
[4] => 1
[5] => 192
[6] => "Woe, Is Me - Hell, or High Water"

If you were to explode() this string, you'd get the artist in 2 nodes and with this songtitle you'd get that one in 2 nodes as well.

The preg_split function has the nice ability to add a limit parameter, thus saying "Split this string on commas, but only do it for the first 7" and this will guarantee that your artist and song title are stuck together on the last index.