dongpochi9741 2016-03-24 13:03 采纳率: 100%
浏览 12

将字符串拆分为php数组

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("/
||
/", $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

  • 写回答

1条回答 默认 最新

  • doutang1992 2016-03-24 18:33
    关注

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

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

    PHP demo at eval.in

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题