2011-05-17 28 views
2

我有一个字符串是这样的:字符串分割成线的PHP通过模式

1. 192.168.122.1 0.89 Bps 2. 192.168.122.10 0.25 Bps 

我想将字符串分割成两个字符串是这样的:

192.168.122.1 0.89 Bps 
192.168.122.10 0.25 Bps 
+4

所以你的研究说?你失败了吗?你试过什么了? – 2011-05-17 05:40:50

+0

我试过爆炸,拆分,分裂所有的东西仍然无法找到方式感谢您的评论 – 2011-05-17 05:42:20

回答

2

preg_split例如:

$str = "1. 192.168.122.1 0.89 Bps 2. 192.168.122.10 0.25 Bps"; 

$split = preg_split("~\s?\d\.\s~", $str, -1, PREG_SPLIT_NO_EMPTY); 

/* 
Array 
(
    [0] => 192.168.122.1 0.89 Bps 
    [1] => 192.168.122.10 0.25 Bps 
) 
*/ 
+0

完美的谢谢smott – 2011-05-17 07:26:42

+0

如果你的'\ d'中有超过9个项目,你可能需要添加'+'量词。 – alex 2011-05-17 23:11:48

4

你可以使用正则表达式。 \d[0-9]字符类的快捷方式,常用于以下表达式中。

preg_match_all('/\d+\. \d+\.\d+\.\d+\.\d+ [\d.]+ Bps/', $str, $matches); 

CodePad

在你的榜样,$matches将包含...

array(1) { 
    [0]=> 
    array(2) { 
    [0]=> 
    string(25) "1. 192.168.122.1 0.89 Bps" 
    [1]=> 
    string(26) "2. 192.168.122.10 0.25 Bps" 
    } 
} 
1

他们是永远保证按照这个顺序?什么是静态的,什么变化等等?这里

实在不够标准,但你可能会看着爆炸的空间,从那里......

<?php 
$str = "1. 192.168.122.1 0.89 Bps 2. 192.168.122.10 0.25 Bps"; 
$arr = explode(" ", $str); 
$your_result = "{$str[1]} {$str[3]} {$str[4]}\n{$str[6]} {$str[7]} {$str[8]}"; 
?>