简体   繁体   中英

splitting string into php array

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, eg:

"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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM