在php中拆分一个字符串

时间:2013-05-28 05:10:04

标签: php

在字符串下方我需要拆分。我试过php爆炸功能

  

$ link =   “7_5_7_http://test.com/folder/images/7_newim/5_car/7_february2013/p/a00/p01/video-1.mov_00:00:09”;

     

$ ex_link = explode('_',$ link);

但它在每个“_”符号之后拆分字符串。但是我需要像这样的结果

$ex_link[0] ==> 7;
$ex_link[1] ==> 5;
$ex_link[2] ==> 7;
$ex_link[3] ==> http://test.com/folder/images/7_newim/5_car/7_february2013/p/a00/p01/video-1.mov;
$ex_link[2] ==> 00:00:09;

任何想法都可以实现这一目标。

提前致谢

3 个答案:

答案 0 :(得分:3)

Explode有第三个参数,为什么人们会把事情复杂化?

$link = "7_5_7_http://test.com/folder/images/7_newim/5_car/7_february2013/p/a00/p01/video-1.mov_00:00:09";
$array = explode('_', $link, 4);
$temp = array_pop($array);
$array = array_merge($array, array_reverse(array_map('strrev', explode('_', strrev($temp), 2)))); // Now it has just become complexer (facepalm)
print_r($array);

<强>输出:

Array
(
    [0] => 7
    [1] => 5
    [2] => 7
    [3] => http://test.com/folder/images/7_newim/5_car/7_february2013/p/a00/p01/video-1.mov
    [4] => 00:00:09
)

<强> Online demo

答案 1 :(得分:2)

使用

preg_match('/(\d)_(\d)_(\d)_([\w:\.\/\/\-]+)_([\d]{2}:[\d]{2}:[\d]{2})/', $link, $matches);

和$匹配:

array(6) {
  [0]=>
  string(95) "7_5_7_http://test.com/folder/images/7_newim/5_car/7_february2013/p/a00/p01/video-1.mov_00:00:09"
  [1]=>
  string(1) "7"
  [2]=>
  string(1) "5"
  [3]=>
  string(1) "7"
  [4]=>
  string(80) "http://test.com/folder/images/7_newim/5_car/7_february2013/p/a00/p01/video-1.mov"
  [5]=>
  string(8) "00:00:09"
}

答案 2 :(得分:1)

这是最简单的一个

$result = preg_split('%_(?=(\d|http://))%si', $subject);