开发者

search for something inside file without open it?

开发者 https://www.devze.com 2023-04-03 18:16 出处:网络
i have about 50 file in some folder i want get all files contain \'Name : EMAD\' inside! example: the php imap library!

i have about 50 file in some folder

i want get all files contain 'Name : EMAD' inside!

example:

the php imap library! they search in thousands messages text files for some word!

i have wrote an stupid function to open file and search inside

$s = scandir('dir');
foreach($s as $file){
$content = file_get_contents($file);
if(strpos($content,'Name : ENAD') !== false)
$matched_files[] = $file;
}

but what if there is thousands of files!

should i open all files???? !!!!

is that possible to se开发者_开发知识库arch for something inside file without open it?

if NO

what is the best and fast way to do that ?php


is that possible to search for something inside file without open it?

of course - no. Where is your common sense? Can you search a refrigerator without opening it?

i have about 50 file in some folder

no problem, it will be fast enough. opening a file is not THAT heavy operation as you imagine.

but what if there is thousands of files!

first have a thousand then come to ask.

what is the best and fast way to do that ?

Store your data in database, not files


Is there a reason you need to use PHP functions? This is exactly what grep was designed to do...

You could always just use PHP's exec() to run an appropriate grep command, such as:

grep -lr 'Name : ENAD' dir

But you might also want to consider (if you're the person creating thousands of files in the first place) whether that is the best way of storing your data - if you usually need the ability to search quickly, you might want to either use a database instead of plain files (e.g. MySQL, PostgreSQL, SQLite, et cetera), or keep a search index (using e.g. Sphinx, Solr, or Lucene).

0

精彩评论

暂无评论...
验证码 换一张
取 消