English 中文(简体)
我如何检查一个阵列中的任何单一元素是否正在扼杀?
原标题:How do I check if any single element from an array is in a string?

I m trying to create a function that searches through the user input to see if they have entered the name of a day. For some reason, this function only seems to return true if any two day names have been entered. This could be something simple, but I can t seem to figure it out.

我的守则是:

<?php
if(isset($_POST[ query ]))
{
    $query=$_POST[ query ];
    $dow=array( monday , tuesday , wednesday , thursday ,
         friday , saturday , sunday );

    foreach($dow as $day)
    {
        if(stripos($query, $day)==FALSE)
        {
        }
        else
        {
            echo"hurray";
        }
    }
}
?>

<html>
    <form action="datefunctiontester.php" method="post">
        <p>Search</p><input type="text" name="query">
        <input type="submit">
    </form>
</html>
最佳回答

你们应当使用

if (stripos($query, $day) === FALSE){

and not

if (stripos($query, $day)==FALSE){

这是因为,如果在问询开始时就找到了名字,那将返回0,因为第一种身份是假的。 检查是否使用<代码>=,检查类型而不是数值。

也可以使用功能,而不是休息。

if (isset($_POST[ query ]))
{
    $query = $_POST[ query ];
    $dow = array( monday , tuesday , wednesday , thursday , friday , saturday , sunday );
    if (in_array($query, $dow) 
    {
        echo"hurray";
    }
}
问题回答

为此:

if (isset($_POST[ query ]) {
   $dow = array( monday , tuesday , wednesday , thursday , friday , saturday , sunday );    
   if (in_array(trim(strtolower($_POST[ query ])), $dow) echo"hurray";
}

在这种情况下无需使用<代码>stripos()。

// Force lower case for comparisons
$query = strtolower( $_POST[ query ] );
$dow = array( monday , tuesday , wednesday , thursday ,
     friday , saturday , sunday );

$found = in_array($query, $dow, true);

if($found)
{
    // do something
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签