如何检查数组中的任何单个元素是否在字符串中?

发布于 2024-12-27 07:46:01 字数 681 浏览 1 评论 0原文

我正在尝试创建一个函数来搜索用户输入以查看他们是否有 输入一天的名称。由于某种原因,此函数似乎仅在输入任意两天的名称时才返回 true。这可能很简单,但我似乎无法弄清楚。

这是我的代码:

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

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.

Here is my code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清风疏影 2025-01-03 07:46:01

=== 不是 == 请参阅 stripos 手动条目

=== not == see the stripos manual entry

流殇 2025-01-03 07:46:01

您应该使用

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

and not

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

这是因为,如果在查询的开头找到该名称,它将返回“0”,因为第一个字符的位置将为 0,这是错误的。要检查它是否存在,请使用 === 它检查类型而不是值。

另外,您可以使用 in_array 函数代替 for 循环。

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

You should use

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

and not

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

This is because, if the name is found in the starting of the query, it will return '0' because the position of first character will be 0 which is false. To check if it exists use === which checks the type and not the value.

Also, you can use the in_array function instead of for loop.

if (isset($_POST['query']))
{
    $query = $_POST['query'];
    $dow = array('monday','tuesday','wednesday','thursday','friday','saturday','sunday');
    if (in_array($query, $dow) 
    {
        echo"hurray";
    }
}
深海里的那抹蓝 2025-01-03 07:46:01

试试这个:

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

Try this:

if (isset($_POST['query']) {
   $dow = array('monday','tuesday','wednesday','thursday','friday','saturday','sunday');    
   if (in_array(trim(strtolower($_POST['query'])), $dow) echo"hurray";
}
谁把谁当真 2025-01-03 07:46:01

在这种情况下,无需使用 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
}

There's no need to use stripos() in this case.

// 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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文