我的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
)
)
答案 0 :(得分: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);
答案 1 :(得分: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);
答案 2 :(得分: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);
?>