Searching string in a text file

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
User avatar
webgroundz
Forum Commoner
Posts: 58
Joined: Thu Jun 21, 2007 1:20 am
Location: Philippines

Searching string in a text file

Post by webgroundz »

@all

Can you give me an example how to search a string in a text file using php.

thanks to phpdn..more power
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Uh... strpos()?
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

staar2
Forum Commoner
Posts: 83
Joined: Fri Apr 06, 2007 2:57 am

Post by staar2 »

1.Read file in array with function File() or you can write your own reading function
2.Check each line with strpost, for that you can use foreach
3. if find word then stop.

This solution is slow, but it depends of file size.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

staar2 wrote:1.Read file in array with function File() or you can write your own reading function
2.Check each line with strpost, for that you can use foreach
3. if find word then stop.

This solution is slow, but it depends of file size.
It makes more sense to use file_get_contents() to read the entire contents into a single string
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Actually, it makes more sense to do it like this:

1. Read 1 line at a time and check if the string is there,
2. If the string is found - stop, else - go to the next line (if exists) and repeat on #1.
Post Reply