php - How to quickly search through a very large list of strings / records on a text file?
Get the solution ↓↓↓I have some text file size(500mb). I need a script that search given string from this text file as first as possible. i was trying
$query="demo";
$FileName = "search.txt";
$fh = fopen($FileName, 'r') or die("Can't open file");
$data = fread($fh, filesize($FileName));
$items = explode(" ", $data);
$Pos = array_search ($query, $items);
if($Pos !== FALSE)
{
echo "Found";
}
else
{
echo "Not Found";
}
Is there any script or algorithm to search more faster? Thanks in advance.
Answer
Solution:
Do not read the entire file.
Read line by line and check if the$query
is contained withing the line (assuming the result can't be in multiple lines):
$query = 'demo';
$fileName = 'search.txt';
$file = fopen($fileName, 'r') or die("Can't open file");
while ($line = fgets($file)) {
if (strpos($query, $line) !== false ) {
$found = true;
break;
}
}
fclose($fh);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: object not found by the @paramconverter annotation.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.