如何检查数组中的任何单个元素是否在字符串中?
我正在尝试创建一个函数来搜索用户输入以查看他们是否有 输入一天的名称。由于某种原因,此函数似乎仅在输入任意两天的名称时才返回 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
=== 不是 == 请参阅 stripos 手动条目
=== not == see the stripos manual entry
您应该使用
and not
这是因为,如果在查询的开头找到该名称,它将返回“0”,因为第一个字符的位置将为 0,这是错误的。要检查它是否存在,请使用
===
它检查类型而不是值。另外,您可以使用 in_array 函数代替 for 循环。
You should use
and not
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.
试试这个:
Try this:
在这种情况下,无需使用
stripos()
。There's no need to use
stripos()
in this case.