1

I store data from an article in a .txt file. A txt file looks like this:

id_20201010120010                           // id of article
Sport                                       // category of article
data/uploads/image-1602324010_resized.jpg   // image of article
Champions League                          // title of article
Nunc porttitor ut augue sit amet maximus... // content of the article 
2020-10-10 12:00                            // date article 
John                                        // author of article
oPXWlZp+op7B0+/v5Y9khQ==                    // encrypted email of author
football,soccer                             // tags of article
true                                        // boolean (SHOULD BE IGNORED WHEN SEARCHING)
false                                       // boolean (SHOULD BE IGNORED WHEN SEARCHING)

For searching in articles, is use this code below:

$searchthis = strtolower('Nunc');
$searchmatches = [];
    
foreach($articles as $article) { // Loop through all the articles
    $handle = @fopen($article, "r");
    if ($handle) {
        while (!feof($handle)) {
            $buffer = fgets($handle);
            if(strpos(strtolower($buffer), $searchthis) !== FALSE) { // strtolower; search word not case sensitive
                $searchmatches[] = $article; // array of articles with search matches                   
            }
            
        }
        fclose($handle);
    }
}

//show results:
if(empty($searchmatches)) { // if empty array
    echo 'no match found';
}
print_r($searchmatches);

This works all fine! But when searching on a word like true, he finds almost all articles because in all articles are the 2 booleans at the last lines. So how can i skip these 2 last lines of the txt file from searching?

john
  • 1,263
  • 5
  • 18
  • I wrote an answer probably at the same time than Nick! always the same problem with stackoverflow. But wouldn't it be more efficient to put all your articles in a database? Probably the files was a historic choice. But it's not to late the push them into a database. I think it will be far easier if you have to add or alter some properties later. – Patrick Janser Nov 19 '20 at 11:57

2 Answers2

2

One way to do this would be to use file to read the entire file into an array, then array_slice to strip the last two elements from the array. You can then iterate through the array looking for the search value. Note you can use stripos to do a case-insensitive search:

foreach ($articles as $article) {
    $data = file($article);
    if ($data === false) continue;
    $data = array_slice($data, 0, -2);
    $search = 'league';
    foreach ($data as $value) {
        if (stripos($value, $search) !== false) {
            $searchmatches[] = $article;
        }
    }
}
Nick
  • 138,499
  • 22
  • 57
  • 95
2

To read your file, instead of using fopen, fgets, etc, like with some C code, just use the file() function. It will read all the file and put it inside an array of lines. Then select the lines where you want to do a search.

<?php

$article = file('article-20201010120010.txt');

// Access each information of the article you need directly.
$id       = $article[0];
$category = $article[1];
// etc...

// Or do it like this with the list() operator of PHP:
list($id, $category, $image, $title, $content, $date, $author, $email_encrypted, $tags, $option_1, $option_2) = $article;

// Now do the insensitive seach in the desired fields.
$search = 'porttitor'; // or whatever typed.

if (($pos = stripos($content, $search)) !== false) {
    print "Found $search at position $pos\n";
} else {
    print "$search not found!\n";
}

Patrick Janser
  • 3,318
  • 1
  • 16
  • 18