LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-28-2007, 12:42 PM   #1
anrea
LQ Newbie
 
Registered: Jan 2007
Posts: 2

Rep: Reputation: 0
php - Read file line by line and change a specific line.


pseudo code:

defined_string = "Anrea"
replace_string = "Anonymous"

Open file "xyz.txt"
Read one line.
Compare line to a defined_string
If they match change line to replace_string
Loop to check for more, til end of file.


I've been staring at this code too long! Does anyone see anything wrong? I did have this working, but seem to have done something to make it quit working.

<?php

$findThis = "Anrea";
$handle = @fopen("xyz.txt", "r"); // Open file form read.

if ($handle) {
while (!feof($handle)) // Loop til end of file.
{
$buffer = fgets($handle, 4096); // Read a line.
if ($buffer <> "Anrea") // Check for string.
{
echo "$buffer"; // If not string show contents.
} else {
$buffer = $findThis; // If string, change it.
echo "$buffer"; // and show changed contents.
}
}
fclose($handle); // Close the file.
}

?>

Thanks for any help!

Anrea
 
Old 01-28-2007, 12:46 PM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,116

Rep: Reputation: 218Reputation: 218Reputation: 218
Hi

fread reads a line - but the last "\n" character is also included in the string.

So you need to call chop or trim on $buffer - or compare it to "Anrea\n"
 
Old 01-28-2007, 01:43 PM   #3
anrea
LQ Newbie
 
Registered: Jan 2007
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you!

Perfect! I knew it had to be something simple....

I appreciate the help.

Anrea
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 08:16 PM
C: fread to read a file line by line until the end Blue_muppet Programming 2 09-19-2008 09:42 AM
read line by line form text file in java. spank Programming 1 10-18-2006 02:46 PM
Script to read line by line from a file kushalkoolwal Programming 20 01-27-2006 04:17 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM


All times are GMT -5. The time now is 04:35 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration