由于您的字符串有 3 种不同类型的数据,由空格分隔,不幸的是,您需要的不仅仅是一个简单的 explode()。
这是一个片段。
// Declare our string
$string = "ubuntu1204gui Client myurl/token=something1 windows7 Gateway myurl/token=token=something2 ubuntu1204gui Server myurl/token=token=something3";
// First Split the spring up into individual items (using the spaces as delimiter)
// http://php.net/manual/en/function.explode.php
$items = explode(' ',$string);
// setup our target arrays
$array1 = $array2 = $array3 = array();
// Reindex our array to start from 1 (so we can use modulus effectively)
array_unshift($items, "temp");
unset($items[0]);
// Then we loop items & put in right array using the help of our friend modulus!
foreach($items as $key=>$item)
{
if($key%3==0)
$array3[] = $item;
else
if($key%3==2)
$array2[] = $item;
else
$array1[] = $item;
}
echo'<pre>';
print_r($array1);
print_r($array2);
print_r($array3);
多田!!
Array
(
[0] => ubuntu1204gui
[1] => Gateway
[2] => ubuntu1204gui
)
Array
(
[0] => Client
[1] => windows7
[2] => Server
)
Array
(
[0] => myurl/token=something1
[1] => myurl/token=token=something2
[2] => myurl/token=token=something3
)