Forum Moderators: coopster

Message Too Old, No Replies

Including a file only when string found

Including a file only when string found

         

winningratings

7:21 am on Dec 26, 2008 (gmt 0)

10+ Year Member



Can anyone help me out with some code in php

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

coopster

5:18 pm on Dec 29, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, winningratings.

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;
}
}
?>