我有一个输出以下内容的数组:
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
答案 0 :(得分:1)
您可以使用preg_match_all
使用regex like this从源中提取相关部分。
$pattern = '/^#[^,\n]+,\K.*/m';
^
anchor匹配多线模式中的行首,该行使用m
flag设置。#[^,\n]+,
使用negated class将#
中的部分与,
匹配。\K
is used to reset报告的匹配开头。我们不想要前一部分。.*
要提取的部分:Any amount any character直到行尾。if(preg_match_all($pattern, $finaloutput, $out) > 0);
print_r($out[0]);