小编mal*_*lly的帖子

PHP代码使用glob排除index.php

问题

我试图从一个名为../health/的文件中显示一个随机页面.在这个文件中有一个index.php文件和118个其他名为php文件的文件.我想从健康文件夹中随机显示一个文件,但我希望它排除index.php文件.

以下代码有时包含index.php文件.我也尝试改变$ exclude行来显示../health/index.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);
?
Run Code Online (Sandbox Code Playgroud)

我尝试过的另一个代码如下

<?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);
?>
Run Code Online (Sandbox Code Playgroud)

此代码还包括index.php文件,而不是显示它将页面显示为错误的页面.

php arrays glob

13
推荐指数
2
解决办法
7657
查看次数

防止html表单字段中的链接

如果表单包含链接,是否可以防止表单提交。我想防止将链接添加到输入:问题和消息字段。

谁能指出正确的信息方向?谢谢

<div class="form">
        <form id="sbwd_contact" method="post" action="http://whatanswered.com/forms/ask/ask.php">
            <em class="error"></em>
            <input type='hidden' name='sfm_form_submitted' value='yes'/>
            <label for="Email">E-Mail: </label>
            <input type="text" id="Email" name="Email" size="30" class="required email" />
            <label for="question">Question: </label>
            <input type="text" id="question" name="question" size="30" class="required" />
            <label for="Message">Additional Info: </label>
            <textarea name="Message" cols="30" rows="6" id="Message" class="required"></textarea>
            <br />
            <p><span>I Agree to the Terms and Conditions
                        <input type="checkbox" name="Terms_and_conditions" value="I agree" class="required"/></span></p>
            <input name="Submit" type="submit" id="Submit" value="Send" />
            <br /><br />
            <p><a href="http://whatanswered.com/terms-and-conditions.php" target="_blank">View our Terms and Conditions</a></p>
        </form>
Run Code Online (Sandbox Code Playgroud)

html forms hyperlink

3
推荐指数
1
解决办法
4793
查看次数

找到闰年

我正在使用PHP代码,开始我正在使用for statment.

for($i=0; $i<4; $i++)
Run Code Online (Sandbox Code Playgroud)

现在我明白$ i = 0从0开始计数而$ i <$ 4意味着从0到低于4.

我想要实现的是为语句添加多个而不是使用多个PHP代码.

for($i=0; $i<4; $i++)
for($i=4; $i<8; $i++)
for($i=8; $i<12; $i++)
for($i=12; $i<16; $i++)
Run Code Online (Sandbox Code Playgroud)

.......等等,以便列出所有结果.

<?php
$day = "";
for($i=0; $i<4; $i++)
{
    $day =  date("d", mktime(0, 0, 0, 2, 29, date("Y")+$i));
    if($day == 29)
    {
        $year = date("Y")+$i;
        break;
    }
}
echo "<p>The next leap year is 29th February $year</p>";    
?>
Run Code Online (Sandbox Code Playgroud)

回声结果将是:

下一个闰年是2016年2月29日

下一个闰年是2020年2月29日

php for-loop

1
推荐指数
2
解决办法
1万
查看次数

标签 统计

php ×2

arrays ×1

for-loop ×1

forms ×1

glob ×1

html ×1

hyperlink ×1