我正在尝试拆分/分解/preg_split 一个字符串,但我想保留分隔符

发布于 2024-12-18 14:36:39 字数 424 浏览 0 评论 0原文

我正在尝试拆分/分解/preg_split 一个字符串,但我想保留分隔符示例:

explode('/block/', '/block/2/page/2/block/3/page/4');

预期结果:

array('/block/2/page/2', '/block/3/page/4');

不确定是否必须循环然后重新为数组值添加前缀,或者是否有更干净的方法。

我已经尝试过 preg_split() 和 PREG_SPLIT_DELIM_CAPTURE 但我得到了一些类似的东西:

array('/block/, 2/page/2', '/block/, 3/page/4');

这不是我想要的。非常感谢任何帮助。

I am trying to split/explode/preg_split a string but I want to keep the delimiter example :

explode('/block/', '/block/2/page/2/block/3/page/4');

Expected result :

array('/block/2/page/2', '/block/3/page/4');

Not sure if I have to loop and then re-prefix the array values or if there is a cleaner way.

I have tried preg_split() with PREG_SPLIT_DELIM_CAPTURE but I get something along the lines of :

array('/block/, 2/page/2', '/block/, 3/page/4');

Which is not what I want. Any help is much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬色的秋千 2024-12-25 14:36:39

您可以像这样使用 preg_match_all

$matches = array();
preg_match_all('/(\/block\/[0-9]+\/page\/[0-9]+)/', '/block/2/page/2/block/3/page/4', $matches);
var_dump( $matches[0]);

输出:

array(2) {
  [0]=>
  string(15) "/block/2/page/2"
  [1]=>
  string(15) "/block/3/page/4"
}

演示

编辑: 这是我能用 preg_split 做的最好的事情。

$array = preg_split('#(/block/)#', '/block/2/page/2/block/3/page/4', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

$result = array();
for( $i = 0, $count = count( $array); $i < $count; $i += 2)
{
    $result[] = $array[$i] . $array[$i + 1];   
}

如果您仍然需要循环来添加分隔符,那么使用正则表达式的开销是不值得的。只需使用爆炸并自己添加分隔符即可:

$delimiter = '/block/'; $results = array();
foreach( explode( $delimiter, '/block/2/page/2/block/3/page/4') as $entry)
{
    if( !empty( $entry))
    {
        $results[] = $delimiter . $entry;
    }
}

演示

最终编辑: 解决了!以下是使用一个正则表达式、preg_splitPREG_SPLIT_DELIM_CAPTURE 的解决方案

$regex = '#(/block/(?:\w+/?)+(?=/block/))#';
$flags = PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY;
preg_split( $regex, '/block/2/page/2/block/3/page/4', -1, $flags);
preg_split( $regex, '/block/2/page/2/order/title/sort/asc/block/3/page/4', -1, $flags);

输出:

array(2) {
  [0]=>
  string(15) "/block/2/page/2"
  [1]=>
  string(15) "/block/3/page/4"
}
array(2) {
  [0]=>
  string(36) "/block/2/page/2/order/title/sort/asc"
  [1]=>
  string(15) "/block/3/page/4"
}

最终演示

You can use preg_match_all like so:

$matches = array();
preg_match_all('/(\/block\/[0-9]+\/page\/[0-9]+)/', '/block/2/page/2/block/3/page/4', $matches);
var_dump( $matches[0]);

Output:

array(2) {
  [0]=>
  string(15) "/block/2/page/2"
  [1]=>
  string(15) "/block/3/page/4"
}

Demo

Edit: This is the best I could do with preg_split.

$array = preg_split('#(/block/)#', '/block/2/page/2/block/3/page/4', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

$result = array();
for( $i = 0, $count = count( $array); $i < $count; $i += 2)
{
    $result[] = $array[$i] . $array[$i + 1];   
}

It's not worth the overhead to use a regular expression if you still need to loop to prepend the delimiter. Just use explode and prepend the delimiter yourself:

$delimiter = '/block/'; $results = array();
foreach( explode( $delimiter, '/block/2/page/2/block/3/page/4') as $entry)
{
    if( !empty( $entry))
    {
        $results[] = $delimiter . $entry;
    }
}

Demo

Final Edit: Solved! Here is the solution using one regex, preg_split, and PREG_SPLIT_DELIM_CAPTURE

$regex = '#(/block/(?:\w+/?)+(?=/block/))#';
$flags = PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY;
preg_split( $regex, '/block/2/page/2/block/3/page/4', -1, $flags);
preg_split( $regex, '/block/2/page/2/order/title/sort/asc/block/3/page/4', -1, $flags);

Output:

array(2) {
  [0]=>
  string(15) "/block/2/page/2"
  [1]=>
  string(15) "/block/3/page/4"
}
array(2) {
  [0]=>
  string(36) "/block/2/page/2/order/title/sort/asc"
  [1]=>
  string(15) "/block/3/page/4"
}

Final Demo

陌若浮生 2024-12-25 14:36:39

请记住:

explode('/block/', '/block/2/page/2/block/3/page/4');

将导致:

array("", "2/page/2", "3/page/4");

您可以使用 preg_match_all 之类的

preg_match_all(":(/block/.*?):", $string); // untested

但只是在前面加上分隔符是一个更清晰的解决方案。

Keep in mind that:

explode('/block/', '/block/2/page/2/block/3/page/4');

Will result in:

array("", "2/page/2", "3/page/4");

You could use a preg_match_all like

preg_match_all(":(/block/.*?):", $string); // untested

But just prepending the delimiter is a much clearer solution.

软糯酥胸 2024-12-25 14:36:39

只需使用前瞻即可。这将匹配字符串中每个爆炸点的零宽度位置。要防止在第一次出现之前拆分,请使用 PREG_SPLIT_NO_EMPTY

代码:(演示

$str = '/block/2/page/2/block/3/page/4';

var_export(
    preg_split('~(?=/block/)~', $str, 0, PREG_SPLIT_NO_EMPTY)
);

输出:

array (
  0 => '/block/2/page/2',
  1 => '/block/3/page/4',
)

Simply use a lookahead. This will match a zero-width position in the string at each point of explosion. To prevent splitting before the first occurrence, use PREG_SPLIT_NO_EMPTY.

Code: (Demo)

$str = '/block/2/page/2/block/3/page/4';

var_export(
    preg_split('~(?=/block/)~', $str, 0, PREG_SPLIT_NO_EMPTY)
);

Output:

array (
  0 => '/block/2/page/2',
  1 => '/block/3/page/4',
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文