-4

I have an array that outputs the following:

Array
( 
    [0] => #EXTM3U 
    [1] => #EXTINF:206,"Weird" Al Yankovic - Dare to be Stupid 
    [2] => E:\Dare to be Stupid.mp3 
    [3] => #EXTINF:156,1910 Fruitgum Company - Chewy, Chewy 
    [4] => E:\Chewy Chewy.mp3 
    [5] => #EXTINF:134,1910 Fruitgum Company - Goody Goody Gumdrops 
    [6] => E:\Goody Goody Gumdrops.mp3 
    [7] => #EXTINF:134,1910 Fruitgum Company - Simon Says 
    [8] => E:\Simon Says.mp3 
    [9] => #EXTINF:255,3 Doors Down - When I'm Gone 
    [10] => E:\When I'm Gone.mp3 [
    11] => #EXTINF:179,? And the Mysterians - 96 Tears**
)

I need to split this array then loop through and save each value to the database, e.g:

"Weird" Al Yankovic - Dare to be Stupid
 Fruitgum Company - Chewy, Chewy
 Save each value above to database individually.

Thanks in advance!


Edit: Added from the comments

Let me try and explain in more detail. I start with a string that looks like this:

#EXTM3U #EXTINF:266,10cc - Dreadlock Holiday
D:\Music - Sorted\Various Artists\De Beste Pop Klassiekers Disc 1\10cc - Dreadlock Holiday.mp3
#EXTINF:263,1919 - Cry Wolf
D:\Music - Sorted\Various Artists\Gothic Rock, Vol. 3 Disc 2\1919 - Cry Wolf.mp3
#EXTINF:318,3 Doors Down - [Untitled Hidden Track]
D:\Music - Sorted\3 Doors Down\Away From The Sun\3 Doors Down - [Untitled Hidden Track].mp3

I'm then trying to strip everything out of this and just have an array of track titles, this is a playlist file for online radio. What I am doing so far:

$finaloutput = $_POST['thestring'];
$finaloutput = str_replace('#EXTINF:','',$finaloutput);
$finaloutput = str_replace('#EXTM3U','',$finaloutput);
$finaloutput = preg_split("/\r\n|\r|\n/", $finaloutput);

foreach ($finaloutput as $value) {
  echo $value; echo '<br>';
}

But I still have these rows remaining, I need to try and do a str_replace between a line break and the end .mp3

D:\Music - Sorted\3 Doors Down\Away From The Sun\3 Doors Down - [Untitled Hidden Track].mp3

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
Jack
  • 91
  • 7
  • And what have you tried so far? You need to show your code, and explain the specific problem you're encountering. – jszobody Mar 24 '16 at 13:05
  • Might be worth having a look at [foreach loops](http://php.net/manual/en/control-structures.foreach.php) – Henders Mar 24 '16 at 13:08
  • I split the initial string into this array using: $finaloutput = preg_split("/\r\n|\r|\n/", $finaloutput); But now I have no idea where to begin to remove the bits I don't need and only loop through what I do need, help! – Jack Mar 24 '16 at 13:14
  • There is lots of "junk" in the array. I think you need to start by cleaning up what goes in before splitting to an array. cleaning while in an array is harder. or well, I think it is. You have not shown us what it is before it becomes an array. Always try to get good data in before you start doing something with the data. And if that is not possible, clean up as soon as possible – Andreas Mar 24 '16 at 13:20
  • Ok let me try and explain in more detail, I start with a string that looks like this: `#EXTM3U #EXTINF:266,10cc - Dreadlock Holiday D:\Music - Sorted\Various Artists\De Beste Pop Klassiekers Disc 1\10cc - Dreadlock Holiday.mp3 #EXTINF:263,1919 - Cry Wolf D:\Music - Sorted\Various Artists\Gothic Rock, Vol. 3 Disc 2\1919 - Cry Wolf.mp3 #EXTINF:318,3 Doors Down - [Untitled Hidden Track] D:\Music - Sorted\3 Doors Down\Away From The Sun\3 Doors Down - [Untitled Hidden Track].mp3` – Jack Mar 24 '16 at 13:38
  • I'm then trying to strip everything out of this and just have an array of track titles, this is a playlist file for online radio. What I am doing so far: – Jack Mar 24 '16 at 13:40
  • '$finaloutput = $_POST['thestring']; $finaloutput = str_replace('#EXTINF:','',$finaloutput); $finaloutput = str_replace('#EXTM3U','',$finaloutput); $finaloutput = preg_split("/\r\n|\r|\n/", $finaloutput); foreach ($finaloutput as $value) { echo $value; echo '
    '; }'
    – Jack Mar 24 '16 at 13:40
  • But I still have these rows remaining, I need to try and do a str_replace between a line break and the end .mp3 'D:\Music - Sorted\3 Doors Down\Away From The Sun\3 Doors Down - [Untitled Hidden Track].mp3' – Jack Mar 24 '16 at 13:41
  • You can try [something like this](https://eval.in/541928) with `preg_match_all` – bobble bubble Mar 24 '16 at 15:41
  • I believe you are on the correct path. I'm typing on my phone and can't help you with an answer but it seems you as if you are geting closer. As bobble says, preg_match could work. look at phpliveregex and you can test it quick and easy – Andreas Mar 24 '16 at 15:56
  • 1
    I did not see the link you posted bobble bubble... good job! You should post it as an answer. – Andreas Mar 24 '16 at 15:58
  • Thanks bobble bubble works a treat! – Jack Mar 24 '16 at 17:16
  • Don't forget to mark the question as answered. On bobbles answer below there should be a V-shaped button (if I remember correct). – Andreas Mar 24 '16 at 20:12

1 Answers1

1

You can extract the relevant parts from source by use of preg_match_all with a regex like this.

$pattern = '/^#[^,\n]+,\K.*/m';
if(preg_match_all($pattern, $finaloutput, $out) > 0);
  print_r($out[0]);

PHP demo at eval.in

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • Wait... isn't 10cc the artist? So [0] and [1] is missing the artist? (It's most likely not my kind of music since I'm not sure, but something in the back of my head says 10cc is a group) – Andreas Mar 24 '16 at 20:17
  • 1
    Well spotted @Andreas, thank you! I did an update and hope it extracts desired parts ( : – bobble bubble Mar 24 '16 at 23:08