Forum Moderators: coopster

Message Too Old, No Replies

executing code only if certain word is found

         

laura2k

3:53 pm on Jun 27, 2004 (gmt 0)



hi,

i have a php code that opens a text file and makes some changes to it. but i only want the code to execute if the following word is found somewhere in the file: "xq33XSDs"

can anyone tell me how i can do this.

thanx

jatar_k

4:28 pm on Jun 27, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> somewhere in the file

is it always in a standard place? Pretty much you would have to read through it until you find it or hit EOF.

laura2k

4:45 pm on Jun 27, 2004 (gmt 0)



no its not in a standard place, it can be anywhere in the file

jatar_k

4:53 pm on Jun 27, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hm, I think you need to read through the whole file then.

Doesn't really seem like a great solution here.

options I can think of off the top of my head

1. use file [ca2.php.net] and then do a if(!in_array [ca2.php.net]()) but if the file is huge then you are loading the whole thing into memory

2. use fgets [ca2.php.net] to read through line by line and use preg_match [ca2.php.net] or some similar string matching function to check each line for the string

3. use exec [ca2.php.net] or passthru [ca2.php.net] to run a command line grep, though this may be a bit of a pain to get going

laura2k

4:55 pm on Jun 27, 2004 (gmt 0)



actually i just remembered, the word is somewhere on the 3rd line, would that be easier to do?

jatar_k

5:10 pm on Jun 27, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



definitely, then just

fopen
fgets 3 times
you now have the third line
test it for your string

if it's there make changes
else fclose