15

Problem

I am trying to display a random page from a file called ../health/ In this file there is a index.php file and 118 other files named php files. I would like to randomly display a file from the health folder but i would like it to exclude the index.php file.

This following code includes the index.php file sometimes. I have also tried altering the $exclude line to show ../health/index.php but still no luck.

<?php
$exclude = array("index.php"); // can add more here later
$answer = array_diff(glob("../health/*.php"),$exclude);
$whatanswer = $answer[mt_rand(0, count($answer) -1)];
include ($whatanswer);
?

Another code i have tried is the following

<?php
$exclude = array("../health/index.php"); // can add more here later
$health = glob("../health/*.php");
foreach ($health as $key => $filename) {
foreach ($exclude as $x) {
if (strstr($filename, $x)) {
unset($whathealth[$key]);
}
}
}
$whathealth = $health[mt_rand(0, count($health) -1)];
include ($whathealth);
?>

This code also includes the index.php file but rather than showing the page it displays the page as an error.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
mally
  • 275
  • 2
  • 4
  • 18
  • I've edited your question to strip out the answer (solution) you've added in. Since you're new to SO, the way it works is that you choose an answer that best solved your problem and you accept it by clicking the check mark next to it. – Ja͢ck Sep 06 '12 at 02:16

3 Answers3

24

The first thing that came to mind is array_filter(), actually it was preg_grep(), but that doesn't matter:

$health = array_filter(glob("../health/*.php"), function($v) {
    return false === strpos($v, 'index.php');
});

With preg_grep() using PREG_GREP_INVERT to exclude the pattern:

$health = preg_grep('/index\.php$/', glob('../health/*.php'), PREG_GREP_INVERT);

It avoids having to use a callback though practically it will likely have the same performance

Update

The full code that should work for your particular case:

$health = preg_grep('/index\.php$/', glob('../health/*.php'), PREG_GREP_INVERT);
$whathealth = $health[mt_rand(0, count($health) -1)];
include ($whathealth);
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • Hello, Thanks for your response but i am a bit new to php and still learning the basics, where does your code fit within my code. Many thanks mally – mally Sep 05 '12 at 15:56
  • @user1649416 I've updated the answer, I think it will make more sense now :) – Ja͢ck Sep 05 '12 at 16:01
  • I know you have got better things to do than teach dummies like me but im still lost!! – mally Sep 05 '12 at 16:13
  • @user1649416 hmm okay, updated again; this is as clear as I can make it, hope it helps :) – Ja͢ck Sep 05 '12 at 16:53
5

To compliment Jack's answer, with preg_grep() you can also do:

$files = array_values( preg_grep( '/^((?!index.php).)*$/', glob("*.php") ) );

This will return an array with all files that do NOT match index.php directly. This is how you could invert the search for index.php without the PREG_GREP_INVERT flag.

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
  • Hello, Thanks for your answer, i did in fact use your code within jacks code and works perfectly, i appreciate your input on this. – mally Sep 05 '12 at 21:55
2

my directory files list is:

$ee = glob(__DIR__.DIRECTORY_SEPARATOR.'*',GLOB_BRACE);

result

Array
(
    [0] => E:\php prj\goroh bot\bot.php
    [1] => E:\php prj\goroh bot\index.php
    [2] => E:\php prj\goroh bot\indexOld.php
    [3] => E:\php prj\goroh bot\test.php
)

i write code to test.php and run it

just use glob like this:

$ee = glob(__DIR__.DIRECTORY_SEPARATOR.'[!{index}]*',GLOB_BRACE);

print_r($ee);

use it for exclude files and directories name start with index

result

(
    [0] => E:\php prj\goroh bot\bot.php
    [1] => E:\php prj\goroh bot\test.php
)

and this for exclude files name end with Old

$ee = glob(__DIR__.DIRECTORY_SEPARATOR.'*[!{Old}].*',GLOB_BRACE);

print_r($ee);

result

Array
(
    [0] => E:\php prj\goroh bot\bot.php
    [1] => E:\php prj\goroh bot\index.php
    [2] => E:\php prj\goroh bot\test.php
)

for you this code work i test in php 8.0 exclude files index.php

$ee = glob(__DIR__.DIRECTORY_SEPARATOR.'*[!{index}].php',GLOB_BRACE);

print_r($ee);