Jump to content


Photo

Search a file/more efficient to use a string or array?


  • Please log in to reply
1 reply to this topic

#1 nick1

nick1
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 02 May 2006 - 03:01 PM

Greetings,

I'm aware PHP can read the entire contents of a file into a string then from there it's possible to search that string for some data.
And I'm also aware that PHP can read the entire contents of a file into an array then from there it's possible to search that array for some data.

But can PHP search a file (such as a .txt or .html file) for a string of data?
If not, then I guess I'm stuck with the first two options I listed. In that case, lets say I have a .txt file with a bunch of text in it. I want to locate a specific phrase within that text file. Is it more efficient (faster results) to read the contents of that .txt file into a string or an array? Then perform the search.

Thanks in advance,

*Nick*

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 02 May 2006 - 03:55 PM

Well, the way to search a file for a string is to read it into either a string or an array and search those. As for the most efficient, if you're just looking for a specific string in the entire file, I'd suggest just reading it all into one string.

In 98% of all other cases, I'd suggest reading it into an array. You can just as easily search the array as well.

Info: PHP Manual





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users