Для блочной системы с плоскими файлами я сохраняю все данные в файле .txt
. В строке 7 txt-файла хранятся теги, разделенные запятыми, как показано ниже:
id_12345678 // 1st line; id line
club // 2nd line; category line
...
sport,athletics // 7th line; tag line
Чтобы захватить все файлы, которые имеют указанный тег c, например sport
и найдите соответствующий файл, в котором основан этот тег, я использую этот код:
$search_tag = 'sport';
$blogfiles = glob($dir.'/*.txt'); // read all the blogfiles
$tag_matches = array();
foreach($blogfiles as $file) { // Loop through all the blogfiles
$lines = file($file, FILE_IGNORE_NEW_LINES); // file into an array
$buffer = $lines[7]; // the tag line
if(strpos(strtolower($buffer), $search_tag) !== FALSE) { // strtolower; tag word not case sensitive
$tag_matches[] = $file; // array of files which have the specific tag inside
}
}
Это отлично работает! $tag_matches
дает мне массив файлов, которые содержат тег sport .
Теперь я хочу искать по нескольким тегам, например, sport,athletics
. Теперь мне нужно получить массив всех файлов, содержащих хотя бы один этих тегов.
Итак, я попробовал:
$search_tag = array('sport','athletics');
...
if(strpos(strtolower($buffer), $search_tag) !== FALSE) { // $search_tag as array of multiple tags does not work anymore ???
$tag_matches[] = $file; // array of files which have the specific tag inside
}
Как мне нужно это делать?