Forum Moderators: coopster
I am trying to include a file but only when a string is found
For example I have several text files stored in a directory called text
There are no other files in this directory except the ones I include on my site - usually randomly
What I want to do now is only include them if a string is found which I will set in the php for the page
There could be a way to include only a maximum of 5 files from the text directory
So I will set a variable say - $keyword = 'HelloWorld' at the top of my page
And then include the files in the text directory which have this keyword
Any ideas much appreciated
A conditional include can be written, yes. Here is the pseudo code logic that may work for you, you'll have to fill in the details.
<?php
$count = 5;
$keyword = 'HelloWorld';
while (looping through directory) {
$fileName = current file name;
$fileContents = read file contents as string;
if ($keyword found in $fileContents) {
require_once $fileName;
}
if (!(--$count)) {
break;
}
}
?>