Page 1 of 1

Searching string in a text file

Posted: Thu Jun 21, 2007 7:14 pm
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

Posted: Thu Jun 21, 2007 7:46 pm
by feyd
Uh... strpos()?

Posted: Fri Jun 22, 2007 2:24 am
by Gente

Posted: Fri Jun 22, 2007 3:25 am
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.

Posted: Fri Jun 22, 2007 3:27 am
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

Posted: Fri Jun 22, 2007 3:36 am
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.