readinf specific line of the file? how to?

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
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

readinf specific line of the file? how to?

Post by alexus »

Hi,
Ok if i have opened the text file that has lines of data in it I want to search line by line if i have line let say containing "hello world" if I do hahe such line I want to be able to delte that line.

How can I do that?

Thanks for help!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

fopen() + fread() or fgets() + some comparison + fclose().
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post by alexus »

But none of those are reading file lines they read bytes, ... I can try to get contect of the file and the do foreach (hmm thats a VB) well separate the string by `/n` and then look (some how ) if one of the parts have my "hellow word" if yes then remove that array alemnt (some how) and write new srting back o file...

Hmm... i have no idea what im doing today... swithcing languages does hurt.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You need to read a little closer:
fgets() wrote:Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first).
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post by alexus »

yes i saw that, still dont see any wat to get lines... like for example if i do bash script for linux i would yse grep to get the line wthat contain my "Hello World". Basicaly if i have whis line "abc Hello Worl abc" and i do grep 'Helo World' I would get returned the full line... thats what im looking for
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

fgets() fetches lines.. your comparison tells you whether the line contains your search term or not. That the same thing grep does.
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post by alexus »

i dont see how to do this from the man, all sample code do something else
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What code have you tried?
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post by alexus »

none, i just looked at it, i dont know where to start.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

If the file you are loading is relatively small, I would use file() to load it into an array and loop using a foreach(), checking your match using if() and substr()

If the file is large use feyd's suggestion, and apply that comparison the same way.

As for feyd's suggestion, what are you having problems with? There are plenty examples for looping through file contents.
Post Reply