php爆炸字符
每隔三个分号(;)如何爆炸?php爆炸字符
示例数据: $ string = piece1; piece2; piece3; piece4; piece5; piece6; piece7; piece8;
例子输出为:
$output[0] = piece1;piece2:piece3;
$output[1] = piece4;piece5;piece6;
$output[2] = piece7;piece8;
谢谢!
我相信你可以用正则表达式来做一些漂亮的事情,但是为什么不分解每一个半色,然后一次添加三个。
$tmp = explode(";", $string);
$i=0;
$j=0;
foreach($tmp as $piece) {
if(! ($i++ %3)) $j++; //increment every 3
$result[$j] .= $piece;
}
我想同样的事情......虽然必须与正则表达式一个漂亮的方式。 – 2009-08-13 22:40:31
那里可以。但是,由于我对正则表达式的处理很糟糕,所以我会避免使用它们,并执行类似 – 2009-08-13 22:44:05
之类的棘手问题,“尽管用正则表达式必须有更漂亮的方法。” hahahahahahahaha – nickf 2009-08-13 23:04:34
也许从不同的角度来看待它。将所有内容分解(),然后再将它合并为三部分。像这样...
$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 ; )
$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;
)
)
如果块数不是3的倍数,则失败。 – mercator 2009-08-13 23:28:16
这里有一个正则表达式的方法,这我不能说是太漂亮。
$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;
)
另一个正则表达式的方法。
<?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
)
)
将按如下所述格式化输出数组:$ m = array_map(create_function('$ a','return $ a [0];'),$ m ); – danamlund 2009-08-14 00:49:19
正则表达式拆分
$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"
}
本质上相同的解决方案与其他那些explode
并再次加入...
$tmp = explode(";", $string);
while ($tmp) {
$output[] = implode(';', array_splice($tmp, 0, 3));
};
最简单的办法我可以认为o f为:
$chunks = array_chunk(explode(';', $input), 3);
$output = array_map(create_function('$a', 'return implode(";",$a);'), $chunks);
第一行+1,第二行-1)(使用'create_function()'是不好的)。从PHP 5开始。3你可以使用lambda函数,但是:'$ output = array_map(function($ a){return implode(';',$ a);},$ chunks);' – mercator 2009-08-15 16:14:59
是的。我想我们可以在现在发布的答案中开始使用5.3特性。 – cletus 2009-08-15 22:45:00
适用于开箱即用的代码+1(即使它已被弃用)。我想要一个快速解决方案,我可以拖放和轻松自定义。就是这样。 – 2015-11-17 17:24:58
+1爱情的问题,以及各种反应:) – karim79 2009-08-13 23:19:03