<?php
function makeGoodSite($str)
{
if (substr($str, 0, 7) == "http://")
{
$str = substr($str, 7);
}
if (substr($str, 0, 4) == "www.")
{
$str = substr($str, 4);
}
$answer = "http://www.".$str;
return $answer;
}
function GetFileList($d)
{
$answer = array();
if ($d != '')
{
chdir($d);
}
$dir = opendir (".");
while ($file = readdir ($dir))
{
if (preg_match('/[0-9].TXT/', $file) or preg_match('/[0-9].txt/', $file))
{
$file = substr($file, 0, -4);
array_push ($answer, $file);
}
}
closedir ($dir);
return $answer;
}
function GetTextFileContent($f)
{
$file = './' . $f . '.txt';
$info = [];
$handle = fopen($file, "r");
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
array_push($info, $buffer);
}
$answer = [
'FIO' => $info[1],
'address' => makeGoodSite($info[3]),
'mail' => $info[0],
'Date' => $info[2]
];
return $answer;
}
function showData($input)
{
echo '<tr><td>'. $input['FIO'] .'</td><td>'. $input['mail'] .'</td><td>'. $input['address'] .'</td><td>' .$input['Date'] .'</td></tr>';
}
function printData($input)
{
echo '<table border="0"><tr><td>FIO</td><td>E-Mail</td><td>Web site</td><td>Date</td></tr>';
array_map("showData",$input);
echo '</table>';
}
function together($d)
{
printData(array_map('GetTextFileContent', GetFileList($d)));
}
function comCheck($input)
{
if (substr($input['mail'], -6, -2) == '.com')
{
return true;
}
return false;
}
function comTest($d)
{
$local = array_map('GetTextFileContent', GetFileList($d));
$info = array_filter($local, 'comCheck');
printData($info);
}
function cmp($a, $b)
{
if (strlen($a['mail']) == strlen($b['mail']))
{
return 0;
}
if (strlen($a['mail']) < strlen($b['mail']))
{
return -1;
}
return 1;
}
function sortBymailLength($d)
{
$local = array_map('GetTextFileContent', GetFileList($d));
usort($local, 'cmp');
printData($local);
}
function makeNewArray($d)
{
$local = array_map('GetTextFileContent', GetFileList($d));
$newArray = [];
for ($i=0; $i < count($local); $i += 1)
{
$newArray[$local[$i]['mail']] = $local[$i];
unset($newArray[$local[$i]['mail']]['mail']);
}
}
function comTestAndSort($d)
{
$local = array_map('GetTextFileContent', GetFileList($d));
usort($local, 'cmp');
$info = array_filter($local, 'comCheck');
printData($info);
}
comTestAndSort('123');