0

我的ips.txt文件包含以下内容:

radie1230: 116.79.254.131
Y_O_L_O: 122.149.157.42
midgetman63: 121.121.14.101, 124.112.115.69, 114.182.51.1, 114.118.55.131, 111.21.22.156
kypero: 121.211.61.118, 117.117.117.46, 121.214.109.247, 111.219.37.75
lythorous: 111.161.225.214, 12.111.184.71, 1.112.201.113, 11.137.214.184, 1.115.21.117, 12.115.241.212, 11.117.116.217

此列表在左侧包含用户名,在右侧由:IP 分隔。IP 以 . 分隔,

创建以下输出的实现是什么?

Array (
    [midgetman63] => Array
        (
            [0] => 121.121.14.101
            [1] => 124.112.115.69
            [2] => 114.182.51.1
            [3] => 114.118.55.131
            [4] => 111.21.22.156
        )
)
4

3 回答 3

2

尝试这个:

$dataFromFile = file('ips.txt');
$dataFromFile = array_map('trim', $dataFromFile);
$result = array();
foreach ($dataFromFile as $line) {
    list($user, $ips) = explode(':', $line, 2);
    $arrayOfIPs = explode(',', $ips);
    $result[trim($user)] = array_map('trim', $arrayOfIPs);
}

var_dump($result);
于 2013-04-03T00:18:04.330 回答
1

如果你在文本文件的末尾没有空格,这应该是可行的。祝你好运。

$text = file_get_contents("ipsontext.txt");
$array = explode("\n",$text);
$finally_array = array();
foreach ($array as $key => $value) {
  $this_key = explode(":",$value);
  $this_values = explode(",",$this_key[1]);
   foreach($this_values as $tv) {
       $finally_array[$this_key[0]][] = $tv;
     }  
 }
 var_dump($finally_array);
于 2013-04-03T00:42:45.533 回答
0

也许这个?

<?php
$output = array();
$search = 'something';
$lines = file('file.txt');
foreach($lines as $line) {
  if(strpos($line, $search) !== false) {
    $d = explode(':', $line); 
    $s = explode(',', $d[1]); 
    foreach $s as $i { array_push($output, $i); }
  }
}

print_r($output);

?>
于 2013-04-03T00:15:50.737 回答