将字符串拆分为php数组

时间:2016-03-24 13:03:03

标签: php arrays sorting preg-split

我有一个输出以下内容的数组:

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**
)

我需要拆分此数组然后循环并将每个值保存到数据库,例如:

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

提前致谢!

修改:从评论中添加

让我试着更详细地解释一下。我从一个看起来像这样的字符串开始:

#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

然后我试图从中删除所有内容并且只有一系列曲目标题,这是在线广播的播放列表文件。到目前为止我在做什么:

$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>';
}

但我仍然保留这些行,我需要尝试在换行符和结束str_replace

之间执行.mp3
  

D:\音乐 - 排序\ 3门向下\远离太阳\ 3门向下 - [无标题隐藏曲目] .mp3

1 个答案:

答案 0 :(得分:1)

您可以使用preg_match_all使用regex like this从源中提取相关部分。

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

PHP demo at eval.in