Page 1 of 1

Php file handling

Posted: Sun Oct 03, 2004 3:35 pm
by actorindp
Hi guys, I'm new to php but this what I'm looking to accomplish.

I want php to open a file which has contents like below:

@340
0101
1010
1111
0000
1100
0011
0110
1001

What I want php to do is to look at the file, find the first '@' then pass whatever is after it on that line to a variable.

Then I want it to take every two lines after that and convert it to one line and send it to a variable

So variable1 =01011010

It would keep doing this until it reached another '@'. Then it would repeat the process over again... can anyone help me out?

Thanks a lot! I hope i made sense!!

-Nick

Posted: Sun Oct 03, 2004 3:45 pm
by John Cartwright
I would look into [php_man]file_get_contents[/php_man] rather than fopen, fread, fclose, and then use [php_man]preg_match[/php_man] to get the number you want

Posted: Sun Oct 03, 2004 3:53 pm
by m3mn0n
[php_man]explode[/php_man]() is another option.

Posted: Sun Oct 03, 2004 6:52 pm
by McGruff
The fgets() fn will read one line at a time.

substr_count() could check if "@" is in the current line.

If you find an "@", dump the next two fgets calls into a var.

Re: Php file handling

Posted: Tue Nov 16, 2004 5:27 am
by ratul
Hi
all, I need the same funtionality, But i want to access the txt file which has html codes.
The below codes works fine for any txt files which do not have the html codes by using fgets(), but do not work for html code.
I have also used fgetss() so that it parse for html code
Pls help.
<?php


?>
<html>
<head>
<title>Using fscanf with Optional Variables</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<p>
<?php
//output file where i want the desired string
$logFile = "output.txt";
//First string Condition
$nametag="<listed business name>\r\n";
//Second string Condition
$nametag1="<td>\r\n";
//Alabama.txt is the input file i want to read from
if (!($fp = fopen("Alabama.txt", "r")))
exit("Unable to open the input file.");
while (!feof($fp))
{
$buffer = fgets($fp, 1024);
if (strcmp($nametag, $buffer) == 0)
{
while (!feof($fp))
{
//echo "hi";
$buffer1 = fgets($fp, 1024);
// print $buffer1;
$fp1= fopen("$logFile","a");
fwrite($fp1, "$buffer1");
if (strcmp($nametag1, $buffer1) == 0)
break;
}
fclose($fp1);
}
}
//print "$buffer<br>\n";
fclose($fp);
?>
</p>
</body>
</html>