|
|
|
| Программа была написана на С# и я попытался перевести ее на PHP. Задача заключается в том, что надо прочитать текстовый файл, и выбрать из него нужную информацию, записать ее в другой текстовый файл и потом удалять каждую 31ю строку! Программа не работает... Не понимаю почему! Кто понимает и может помочь помогите пожалуйста!!!
<?php
function isDigit( $c ) // на цыфру
{
if ($c == '0' || $c == '1' || $c == '2' || $c == '3' || $c == '4' ||
$c == '5' || $c == '6' || $c == '7' || $c == '8' || $c == '9')
{
return true;
}
else {return false;}
}
function isDate1( $s ) //26.09.2012
{
if (strlen($s) != 10)
{
return false;
}
if (isDigit($s[0]) === true && isDigit($s[1]) === true &&
$s[2] == '.' &&
isDigit($s[3]) === true && isDigit($s[4]) === true &&
$s[5] == '.' &&
isDigit($s[6]) === true && isDigit($s[7]) === true &&
isDigit($s[8]) === true && isDigit($s[9]) === true)
{
return true;
}
else {return false;}
}
function isDate2( $s ) // 6.09.2012
{
if (strlen($s) != 9)
{
return false;
}
if (isDigit($s[0]) === true &&
$s[1] == '.' &&
isDigit($s[2]) === true && isDigit($s[3]) === true &&
$s[4] == '.' &&
isDigit($s[5]) === true && isDigit($s[6]) === true &&
isDigit($s[7]) === true && isDigit($s[8]) === true)
{
return true;
}
else {return false;}
}
function isCurs( $s ) // 8.27 UAH
{
if($s[strlen($s)-3] == 'U' && $s[strlen($s)-2] == 'A' && $s[strlen($s)-1] == 'H')
{
return true;
}
else {return false;}
}
$countCurs = 0;
$countDate = 0;
$file_read = fopen('read.txt', "r");
while (!feof($file_read))
{
$str = fgets($file_read);
//на тег
if( strpos($str, "<td") >= 0 )
{
$i = strpos($str, "<td");
while ($str[$i] != ">")
{
$i++;
}
$j = $i;
while($str[$j] != '<')
{
$j++;
}
$res = substr($str ,$i+1, $j-$i-1);
$res = str_replace($res," ", "");
//дозапись в файл
$file_result = fopen ("result.txt","r+");
if ( !$file_result )
{
echo("Ошибка открытия файла");
}
else
{
if(isDate1($res) === true || isDate2($res) === true)
{
$countDate++;
if($countDate == 1) { fwrite($file_result, $res); }
}
if(isCurs($res) === true)
{
$countCurs++;
if($countCurs == 1) { fwrite($file_result, " USD $res"); }
if($countCurs == 2) { fwrite($file_result, " EUR $res"); }
if($countCurs == 3) {fwrite($file_result, " AMADEUS $res\n"); }
}
}
}
//Удалим 31 скроку из файла
$max = 30;
$file = file("result.txt");
for($i = 0; $i < sizeof($file); $i++)
{
if($i > $max) unset($file[0]);
}
$res = fopen("result.txt", "a");
fwrite($res, implode("", $file));
}
fclose ($file_read);
fclose($file_result);
fclose($res);
?>
|
| |
|
|
|
|
|
|
|
для: roni10
(01.10.2012 в 03:04)
| | Если не сложно прикрепите кусочек read.txt файла, чтобы можно было воспроизвести ситуацию. | |
|
|
|