PHP开发实例大全(提高卷) 中文完整pdf扫描版[244MB]
<?php
$hosts = file('C:/windows/system32/drivers/etc/hosts');
$group = array();
foreach ($hosts as $line)
{
if (trim($line) == '' || $line{0} == '#') continue;
$line = preg_split('/\s+/', $line);
$ip = array_shift($line);
foreach ($line as $h)
{
if (trim($h) == '') continue;
$g = explode('.', $h, 2);
$p = $g[0];
$g = isset($g[1]) ? $g[1] : $g[0];
$group[$g][$ip][] = array(
$p, $g, $h, $ip
);
}
}
$ul = array('<ul>');
foreach ($group as $name=>$g)
{
$ul[] = "<li class='bg1'><span>{$name}</span><ul>";
foreach ($g as $ip=>$hosts)
{
$ul[] = "<li class='bg2'><span>{$ip}</span><ul>";
foreach ($hosts as $h)
{
$ul[] = "<li class='bg3'><span>{$h[0]}({$h[2]})</span></li>";
}
$ul[] = '</ul></li>';
}
$ul[] = '</ul></li>';
}
$ul[] = "<ul>";
$ul = implode('', $ul);
require 'hosts.phtml';
转载请注明:谷谷点程序 » php查看windows的hosts的文件的方法