每三个字符实例后分割一个字符串

发布于 2024-08-02 06:29:31 字数 302 浏览 5 评论 0原文

如何在每三个分号 (;) 之后的位置分解字符串?

示例数据:

$string = 'piece1;piece2;piece3;piece4;piece5;piece6;piece7;piece8;';

所需输出:

$output[0] = 'piece1;piece2:piece3;'

$output[1] = 'piece4;piece5;piece6;'

$output[2] = 'piece7;piece8;'

请注意,保留尾随分号。

How can I explode a string at the position after every third semicolon (;)?

example data:

$string = 'piece1;piece2;piece3;piece4;piece5;piece6;piece7;piece8;';

Desired output:

$output[0] = 'piece1;piece2:piece3;'

$output[1] = 'piece4;piece5;piece6;'

$output[2] = 'piece7;piece8;'

Notice the trailing semicolons are retained.

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

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

发布评论

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

评论(10

℡寂寞咖啡 2024-08-09 06:29:31

我确信您可以使用正则表达式做一些巧妙的事情,但为什么不直接分解每个半色,然后一次添加三个。

$tmp = explode(";", $string);
$i=0;
$j=0;

foreach($tmp as $piece) {
   if(! ($i++ %3)) $j++;   //increment every 3 
   $result[$j] .= $piece;
}

I am sure you can do something slick with regular expressions, but why not just explode the each semicolor and then add them three at a time.

$tmp = explode(";", $string);
$i=0;
$j=0;

foreach($tmp as $piece) {
   if(! ($i++ %3)) $j++;   //increment every 3 
   $result[$j] .= $piece;
}
逆夏时光 2024-08-09 06:29:31

我能想到的最简单的解决方案是:

$chunks = array_chunk(explode(';', $input), 3);
$output = array_map(create_function('$a', 'return implode(";",$a);'), $chunks);

Easiest solution I can think of is:

$chunks = array_chunk(explode(';', $input), 3);
$output = array_map(create_function('$a', 'return implode(";",$a);'), $chunks);
天煞孤星 2024-08-09 06:29:31

本质上与其他爆炸并再次加入的解决方案相同......

$tmp = explode(";", $string);

while ($tmp) {
    $output[] = implode(';', array_splice($tmp, 0, 3));
};

Essentially the same solution as the other ones that explode and join again...

$tmp = explode(";", $string);

while ($tmp) {
    $output[] = implode(';', array_splice($tmp, 0, 3));
};
太傻旳人生 2024-08-09 06:29:31
$string = "piece1;piece2;piece3;piece4;piece5;piece6;piece7;piece8;piece9;";
preg_match_all('/([A-Za-z0-9\.]*;[A-Za-z0-9\.]*;[A-Za-z0-9\.]*;)/',$string,$matches);

print_r($matches);

Array
(
    [0] => Array
        (
            [0] => piece1;piece2;piece3;
            [1] => piece4;piece5;piece6;
            [2] => piece7;piece8;piece9;
        )

    [1] => Array
        (
            [0] => piece1;piece2;piece3;
            [1] => piece4;piece5;piece6;
            [2] => piece7;piece8;piece9;
        )

)
$string = "piece1;piece2;piece3;piece4;piece5;piece6;piece7;piece8;piece9;";
preg_match_all('/([A-Za-z0-9\.]*;[A-Za-z0-9\.]*;[A-Za-z0-9\.]*;)/',$string,$matches);

print_r($matches);

Array
(
    [0] => Array
        (
            [0] => piece1;piece2;piece3;
            [1] => piece4;piece5;piece6;
            [2] => piece7;piece8;piece9;
        )

    [1] => Array
        (
            [0] => piece1;piece2;piece3;
            [1] => piece4;piece5;piece6;
            [2] => piece7;piece8;piece9;
        )

)
摇划花蜜的午后 2024-08-09 06:29:31

也许从不同的角度来看待它。将其全部分解,然后将其重新组合成三元组。就像这样...

$str = "1;2;3;4;5;6;7;8;9";
$boobies = explode(";", $array);
while (!empty($boobies))
{
  $foo = array();
  $foo[] = array_shift($boobies);
  $foo[] = array_shift($boobies);
  $foo[] = array_shift($boobies);
  $bar[] = implode(";", $foo) . ";";
}

print_r($bar);

数组

[0] => 1;2;3;
[1] => 4;5;6;
[2] => 7;8;9;

Maybe approach it from a different angle. Explode() it all, then combine it back in triples. Like so...

$str = "1;2;3;4;5;6;7;8;9";
$boobies = explode(";", $array);
while (!empty($boobies))
{
  $foo = array();
  $foo[] = array_shift($boobies);
  $foo[] = array_shift($boobies);
  $foo[] = array_shift($boobies);
  $bar[] = implode(";", $foo) . ";";
}

print_r($bar);

Array
(
[0] => 1;2;3;
[1] => 4;5;6;
[2] => 7;8;9;
)

紫竹語嫣☆ 2024-08-09 06:29:31

这是一个正则表达式方法,我不能说它看起来太好了。

$str='';
for ($i=1; $i<20; $i++) {
    $str .= "$i;";
}

$split = preg_split('/((?:[^;]*;){3})/', $str, -1,
                    PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

输出:

Array
(
    [0] => 1;2;3;
    [1] => 4;5;6;
    [2] => 7;8;9;
    [3] => 10;11;12;
    [4] => 13;14;15;
    [5] => 16;17;18;
    [6] => 19;
)

Here's a regex approach, which I can't say is all too good looking.

$str='';
for ($i=1; $i<20; $i++) {
    $str .= "$i;";
}

$split = preg_split('/((?:[^;]*;){3})/', $str, -1,
                    PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

Output:

Array
(
    [0] => 1;2;3;
    [1] => 4;5;6;
    [2] => 7;8;9;
    [3] => 10;11;12;
    [4] => 13;14;15;
    [5] => 16;17;18;
    [6] => 19;
)
古镇旧梦 2024-08-09 06:29:31

另一种正则表达式方法。

<?php
$string = 'piece1;piece2;piece3;piece4;piece5;piece6;piece7;piece8';
preg_match_all('/([^;]+;?){1,3}/', $string, $m, PREG_SET_ORDER);
print_r($m);

结果:

Array
(
    [0] => Array
        (
            [0] => piece1;piece2;piece3;
            [1] => piece3;
        )

    [1] => Array
        (
            [0] => piece4;piece5;piece6;
            [1] => piece6;
        )

    [2] => Array
        (
            [0] => piece7;piece8
            [1] => piece8
        )

)

Another regex approach.

<?php
$string = 'piece1;piece2;piece3;piece4;piece5;piece6;piece7;piece8';
preg_match_all('/([^;]+;?){1,3}/', $string, $m, PREG_SET_ORDER);
print_r($m);

Results:

Array
(
    [0] => Array
        (
            [0] => piece1;piece2;piece3;
            [1] => piece3;
        )

    [1] => Array
        (
            [0] => piece4;piece5;piece6;
            [1] => piece6;
        )

    [2] => Array
        (
            [0] => piece7;piece8
            [1] => piece8
        )

)
蘸点软妹酱 2024-08-09 06:29:31

正则表达式拆分

$test = ";2;3;4;5;6;7;8;9;10;;12;;14;15;16;17;18;19;20";
// match all groups that:
// (?<=^|;) follow the beginning of the string or a ;
// [^;]*  have zero or more non ; characters
// ;? maybe a semi-colon (so we catch a single group)
// [^;]*;? again (catch second item)
// [^;]* without the trailing ; (to not capture the final ;)
preg_match_all("/(?<=^|;)[^;]*;?[^;]*;?[^;]*/", $test, $matches);
var_dump($matches[0]);


array(7) {
  [0]=>
  string(4) ";2;3"
  [1]=>
  string(5) "4;5;6"
  [2]=>
  string(5) "7;8;9"
  [3]=>
  string(6) "10;;12"
  [4]=>
  string(6) ";14;15"
  [5]=>
  string(8) "16;17;18"
  [6]=>
  string(5) "19;20"
}

Regex Split

$test = ";2;3;4;5;6;7;8;9;10;;12;;14;15;16;17;18;19;20";
// match all groups that:
// (?<=^|;) follow the beginning of the string or a ;
// [^;]*  have zero or more non ; characters
// ;? maybe a semi-colon (so we catch a single group)
// [^;]*;? again (catch second item)
// [^;]* without the trailing ; (to not capture the final ;)
preg_match_all("/(?<=^|;)[^;]*;?[^;]*;?[^;]*/", $test, $matches);
var_dump($matches[0]);


array(7) {
  [0]=>
  string(4) ";2;3"
  [1]=>
  string(5) "4;5;6"
  [2]=>
  string(5) "7;8;9"
  [3]=>
  string(6) "10;;12"
  [4]=>
  string(6) ";14;15"
  [5]=>
  string(8) "16;17;18"
  [6]=>
  string(5) "19;20"
}
回心转意 2024-08-09 06:29:31
<?php
$str = 'piece1;piece2;piece3;piece4;piece5;piece6;piece7;piece8;';
$arr = array_map(function ($arr) {
    return implode(";", $arr);
}, array_chunk(explode(";", $str), 3));
var_dump($arr);

输出

 array(3) {
  [0]=>
  string(20) "piece1;piece2;piece3"
  [1]=>
  string(20) "piece4;piece5;piece6"
  [2]=>
  string(14) "piece7;piece8;"
}
<?php
$str = 'piece1;piece2;piece3;piece4;piece5;piece6;piece7;piece8;';
$arr = array_map(function ($arr) {
    return implode(";", $arr);
}, array_chunk(explode(";", $str), 3));
var_dump($arr);

outputs

 array(3) {
  [0]=>
  string(20) "piece1;piece2;piece3"
  [1]=>
  string(20) "piece4;piece5;piece6"
  [2]=>
  string(14) "piece7;piece8;"
}
黎夕旧梦 2024-08-09 06:29:31

与 @Sebastian 之前的回答类似,我建议使用重复模式的 preg_split() 。不同之处在于,通过使用非捕获组并附加 \K 来重新启动全字符串匹配,您可以省去编写 PREG_SPLIT_DELIM_CAPTURE 标志。

代码:(Demo)

$string = 'piece1;piece2;piece3;piece4;piece5;piece6;piece7;piece8;';

var_export(preg_split('/(?:[^;]*;){3}\K/', $string, 0, PREG_SPLIT_NO_EMPTY));

可以找到一种类似的技术,用于在每 2 个事物之后进行拆分此处。该代码片段实际上在最后一个空格字符之前写入了 \K ,以便在分割时消耗尾随空格。

Similar to @Sebastian's earlier answer, I recommend preg_split() with a repeated pattern. The difference is that by using a non-capturing group and appending \K to restart the fullstring match, you can spare writing the PREG_SPLIT_DELIM_CAPTURE flag.

Code: (Demo)

$string = 'piece1;piece2;piece3;piece4;piece5;piece6;piece7;piece8;';

var_export(preg_split('/(?:[^;]*;){3}\K/', $string, 0, PREG_SPLIT_NO_EMPTY));

A similar technique for splitting after every 2 things can be found here. That snippet actually writes the \K before the last space character so that the trailing space is consumed while splitting.

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