1

I'm trying to get 2 random results from an array of files in the "related" directory. I've managed to pull two results randomly from the directory but I need to avoid certain results depending on a variable relating to a specific file name.

My code so far is:

$foo = "bar.php";
function random_file($dir) {
    $files = opendir($dir . '/*.php');
    $rand_files = array_rand($files, 2);
    return array(include $files[$rand_files[0]], include $files[$rand_files[1]]);
}
list($file_1,$file_2) = random_file("related"); 

I'm trying to pull two random results but avoid the file: bar.php. Does anyone know of a way to omit certain results from the array as I can't find anything online even close?

9
  • you exclusion criteria is based on a certain string or an exclusion list array? Commented Nov 18, 2016 at 16:31
  • opendir() doesnt work here, use scandir() and then filter the array. Did that code work??? Commented Nov 18, 2016 at 16:32
  • Yeah, I'll be adding this code to many pages and the string will vary depending on the page Commented Nov 18, 2016 at 16:32
  • @ElzoValugi I'd assume on a certain string according to the question. Commented Nov 18, 2016 at 16:32
  • @E.Nathan if Yeah was for me. That code can't work. opendir() dont work in that way and also array(include $file) works only if the php file end with an return $whatever; statement Commented Nov 18, 2016 at 16:34

3 Answers 3

1

You can use glob function with a specific regex to only select names that are a match for you. This will limit your initial $files variable to results that do satisfy your condition and you can continue and do the random sampling without modifications.

// entries containing foo will not be included
function random_file($dir) {
    $files = glob("^(?!bar.php)*");
    $rand_files = array_rand($files, 2);
    return array(include $files[$rand_files[0]], include $files[$rand_files[1]]);
}
list($file_1,$file_2) = random_file("related");
Sign up to request clarification or add additional context in comments.

4 Comments

This looks like what I need but adding the regex in is resulting in no results coming back at all. Trying to figure out why now.
This does seem a bit more elegant than my solution. I haven't tested it personally but as long as your regex is set right I see no reason this wouldn't work as well.
An extra parameter for the basedir (starting folder for glob) would be great. But, whatever Question is 100% answered :)
I think the problem I'm having is that it's not searching the directory I need now. It works when I use glob($dir . '/*.php'); but when removing $dir . and adding the regex it's saying that nothing is being found in the array. I'll read up on the documentation and try fix it. Thanks everyone!
0

You also need to consider directories such as '.' and '..'. I think a switch statement and an unset() for those values in your overall array would work. Then once you have an array with files not including what you don't want. You can then pull two randoms and return that array.

This code may not be 100% perfect but should get you in the right direction.

function random_file($dir) {
$fileArray = array();

if (is_dir($dir)) {
    if ($dh = opendir($dir . '/*.php')) {
        while (($file = readdir($dh)) !== false) {
            $fileArray = array_push($fileArray, $file)
        }

        for( $i = 0; $i < count($fileArray); $i++ ) {
            switch($fileArray) {
                case '.':
                    unset($array[$i]);
                    break;
                case '..':
                    unset($array[$i]);
                    break;
                case 'bar.php':
                    unset($array[$i]);
                    break;
            }
        }
    }
    closedir($dh);
}

$rand_files_keys = array_rand($fileArray, 2);
$rand_files = $fileArray[$rand_files_keys[0]];
$rand_files = $fileArray[$rand_files_keys[1]];
return $rand_files;

1 Comment

If $dir is given, is it not better to return the full path of the file? He want to include them! :)
0

try this. It will keep randomizing your array until it selects two records excluding the bar.php

$rand_files = array_rand($files, 2);
while(in_array("bar.php",$rand_files))
{
  $rand_files = array_rand($files, 2);
}

1 Comment

While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please include an explanation for your code, as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Flaggers / reviewers: For code-only answers such as this one, downvote, don't delete!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.