Jump to content

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


nick1

Recommended Posts

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*
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.