Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_拆分和多个分隔符_Php_Regex_Preg Split - Fatal编程技术网

Php preg_拆分和多个分隔符

Php preg_拆分和多个分隔符,php,regex,preg-split,Php,Regex,Preg Split,测试字符串 1-gc-communications/edit/profile\u图片 让我先说第一个-之前的第一个数字将是我需要提取的ID。从第一个-到第一个/将是我需要提取的“名称”。之后的一切我都不在乎。因此,我正在寻找的一个例子是: 数组([0]=>1[1]=>gc通信[2]=>/edit/profile\u picture) 我能想到的最好的模式是以下模式(以及它们的结果-限制为3) 模式:/-|编辑\/profile\u图片/结果:数组([0]=>1[1]=>gc[2]=>commu

测试字符串

1-gc-communications/edit/profile\u图片

让我先说第一个
-
之前的第一个数字将是我需要提取的ID。从第一个
-
到第一个
/
将是我需要提取的“名称”。之后的一切我都不在乎。因此,我正在寻找的一个例子是:

数组([0]=>1[1]=>gc通信[2]=>/edit/profile\u picture)

我能想到的最好的模式是以下模式(以及它们的结果-限制为3)

模式:
/-|编辑\/profile\u图片/
结果:
数组([0]=>1[1]=>gc[2]=>communications/edit/profile\u图片)

^这一个是有缺陷的,因为它做两个破折号

模式:
/~-~~编辑\/profile\u图片/
结果:
数组([0]=>1-gc-communications/[1]=>)

^大失败

我知道我可以做一个2限制,只需在第一个
/
上突破,然后在生成的数组上执行
预分割
,但我希望有一种方法可以用一行实现这一点

提前谢谢

如果这是不可能的,我愿意接受其他“一行”解决方案


工作代码。谢谢
preg#u split(“#([^-]+)-([^/]+)/(.*)#)”,“1-gc-communications/edit/profile_picture”,-1,preg#u split_DELIM|u CAPTURE | preg#u split| NO|u EMPTY)

结果

Array([0]=>1[1]=>gc通信[2]=>edit/profile\u picture)

试试这个

$str = '1-gc-communications/edit/profile_picture';
$match = preg_split('#([^-]+)-([^/]+)/(.*)#', $str, 0, PREG_SPLIT_DELIM_CAPTURE);    
print_r($match);
归来

 array (
    0 => '',
    1 => '1',
    2 => 'gc-communications',
    3 => 'edit/profile_picture',
    4 => '',
  )

令人惊叹的!最后添加了
| PREG_SPLIT_NO_EMPTY
,以删除空白结果。将用我的结果编辑帖子。