1

下面的字符串我需要拆分。我尝试使用 php 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";

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

任何想法来实现这一点。

提前致谢

4

3 回答 3

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
)

在线演示

于 2013-05-28T05:23:06.543 回答
2

采用

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

和 $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"
}
于 2013-05-28T05:16:49.383 回答
1

这个是最简单的

$result = preg_split('%_(?=(\d|http://))%si', $subject);
于 2013-05-28T05:47:07.603 回答