Php file handling

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
actorindp
Forum Newbie
Posts: 1
Joined: Sun Oct 03, 2004 3:31 pm

Php file handling

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

[php_man]explode[/php_man]() is another option.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
ratul
Forum Newbie
Posts: 1
Joined: Tue Nov 16, 2004 5:20 am

Re: Php file handling

Post 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>
Post Reply