本文介绍了PHP爆炸字符的第三个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将每个第三个分号(;)炸开?

How can I explode every third semicolon (;) as a piece?

示例数据:$ string = piece1; piece2; piece3; piece4; piece5; piece6; piece7; piece8;

example data:$string = piece1;piece2;piece3;piece4;piece5;piece6;piece7;piece8;

示例输出为:

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

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

$output[2] = piece7;piece8;

谢谢!

推荐答案

我确信您可以使用正则表达式做一些漂亮的事情,但是为什么不爆炸每个半彩色然后一次添加三个.

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

这篇关于PHP爆炸字符的第三个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-30 02:28