Forum Moderators: coopster

Message Too Old, No Replies

Edit flat file via php

Remove a line and other lines associated w/ it

         

awreneau

4:54 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



Have a web front end to access a flat text file, seperator is \r\n. The file looks like this:

blank line
#Branch Number ###
#Branch City, Branch State
#Description
#User Name
IP
blank line

Basically, postfix reads from this list to check if the device attempting SMTP is "allowed". If so, mail is sent, otherwise rejected.

Users can populate this file thru the web front end, however I've not allowed them to delete anything, that need has now become required.

I want to have the user enter the IP they want to delete and then associate the 4 rows above the IP, as well as the blank line, with the deletion.

New to PHP so this is a bit of a leap for me.

awreneau

4:57 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



Well, I wanted to edit this and qualify a few things. I've got the logic in place to open the file and that works. Getting the data deleted that I don't want is the problem.

Thought about reading the file into an array and excluding the data I didn't want and re-write the file w/ a for loop. Not sure if that will work.

MattAU

9:14 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



Thought about reading the file into an array and excluding the data I didn't want and re-write the file w/ a for loop. Not sure if that will work.

Yep, that's as good a way as any to do it. You can explode the data into an array using "\r\n\r\n" so it'll be split on the blank lines.

$data = file_get_contents('file.txt');
$data_array = explode("\r\n\r\n",$data);
foreach($data_array as $key => $value)
{
// The following checks if the IP address you're looking for appears anywhere in the given block of data.
// It's not very neat but it should work as long as the branch numbers aren't formated the same way as IPs.
if(strpos($value,$ip_address) !== false)
{
unset($data_array[$key]);
}
}

// Rejoin the data into one string

// Write the file

awreneau

3:41 pm on Sep 11, 2008 (gmt 0)

10+ Year Member



MattAU, thanks for that. I'll give that shot.

awreneau

9:32 pm on Sep 24, 2008 (gmt 0)

10+ Year Member



MattAU,

This isnt working for me, this is just a mock up of what I hope to have and the formatting isnt clean but I think you'll get the idea.

The allowed_relay_ip is layed out as follows (actual snippet)

#558 somewhere
#printer
#wes
#09/10/2008, 02:31 PM
45.45.45.1

#413 Columbia, MO
#HP M4345
#Shaw
#09/12/2008, 01:24 PM
172.23.183.137

The actual code at present, I'm experimenting a bit, is below:

<?php
$ip_address = '172.23.42.44';
$data = file_get_contents('/etc/postfix/allowed_relay_ip');
$data_array = explode("\r\n\r\n",$data);
foreach($data_array as $key => $value)
{
// The following checks if the IP address you're looking for appears anywhere in the given block of data.
// It's not very neat but it should work as long as the branch numbers aren't formatted the same way as IPs.
if(strpos($value,$ip_address) !== false)
{
unset($data_array[$key]);
}
else
{
print $value;
print $key;

}
}
?>

No matter what I set the IP address to it never finds it in the array. For instance, I set the IP address in the above code and have grep the file and indeed it exists. Yet it never shows up when I browse to this page.

The print statements in the else statement are my attempts at getting something to show up.

Not familiar w/ strpos and my PHP in a Nutshell hasn't helped me on this one. Certain this is just an oversight on my part.

I'd appreciate you chiming in w/ some ideas.

Thanks

MattAU

12:04 am on Sep 25, 2008 (gmt 0)

10+ Year Member



That code should work awreneau.

I changed

$ip_address = '172.23.42.44';

to

$ip_address = '172.23.183.137';

And it found the IP. Of course it can only find something that's actually there... :)

dreamcatcher

7:01 am on Sep 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, you can use file [uk.php.net] to read the contents of the file into an array. No need to read and explode. Each to his own though.

dc

awreneau

3:25 pm on Sep 25, 2008 (gmt 0)

10+ Year Member



MattAU,

This is the frustrating part of coding. I have grepped the file and the CLI and it displays the IP, running the script using the same IP, returns nothing.

I'll try using the file option instead of file_get_contents.

What did you use to verify that the IP was found, the same print statements as I used?

max_jessop

11:31 am on Oct 22, 2008 (gmt 0)

10+ Year Member



Hi, I am hoping I can use the above examples to solve my problem? I have a .txt file with blocks of text separated by blank lines. I need to save each block into separate .txt files on the server.
I am a complete novice and so far only managed to load the .txt into an array and echo it back. Any help would be appreciated thanks.