PHP代码排除索引。php使用一滴

时间:2022-09-02 00:22:46

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.

我试图从一个叫做…的文件中随机显示一个页面。/health/在这个文件中有一个索引。php文件和其他118个文件命名为php文件。我希望从health文件夹中随机显示一个文件,但我希望它排除索引。php文件。

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文件。我也尝试过改变$排除线以显示.. ./健康/指数。但还是不走运。

<?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.

此代码还包含索引。php文件,但不是显示页面,而是显示页面为错误。

2 个解决方案

#1


15  

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

首先想到的是array_filter(),实际上它是preg_grep(),但这并不重要:

$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:

使用preg_grep()使用PREG_GREP_INVERT排除模式:

$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);

#2


4  

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

用preg_grep()来恭维Jack的回答,您还可以这样做:

$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.

这将返回一个包含所有不匹配索引的文件的数组。php直接。这就是如何求逆索引的方法。没有PREG_GREP_INVERT标志的php。

#1


15  

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

首先想到的是array_filter(),实际上它是preg_grep(),但这并不重要:

$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:

使用preg_grep()使用PREG_GREP_INVERT排除模式:

$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);

#2


4  

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

用preg_grep()来恭维Jack的回答,您还可以这样做:

$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.

这将返回一个包含所有不匹配索引的文件的数组。php直接。这就是如何求逆索引的方法。没有PREG_GREP_INVERT标志的php。