Page 1 of 1

Problems with array_search and file()

Posted: Thu Jun 02, 2005 11:57 pm
by jaymoore_299
anyone else have problems using array_search to seek in arrays created by file()?

I have tried creating my own array and I had no problems getting the correct location of the first instance, however when I used a file() array, it fails.

The file in question isn't the problem. To test this I used a file that I successfully loaded just before, and printed the output. file() is reading the file, but array_search does not seem to work with that array. Other arrays seem to work fine.

Posted: Fri Jun 03, 2005 12:22 am
by Burrito
have you tried some of the other array functions on the array created by file()?

could also be that the values created in the array are larger than the scope you're searching as I think file() generates new keys based on new lines...

Posted: Fri Jun 03, 2005 12:43 am
by Syranide
File returns an array based on the entire line, including newline... which is why you cannot search and find anything... I'm not sure if it uses CRLF on windows though or if it's faked.

Posted: Fri Jun 03, 2005 12:49 am
by jaymoore_299
I'm trying to find a way to find the first instance of a certain string pattern, such as "####" in a file.
I can get the file contents easily with file(), but is there a way that I can change that array to work with array_search, or use some other method to get a line from a file one by one to process?

Posted: Fri Jun 03, 2005 1:08 am
by Syranide
forach-loop the contents and search using strpos or so (and perhaps trim of the rightmost character if it is a newline).