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*
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.