我试图制作一个简单的搜索框,使用strpos
检查输入的关键字是否与变量匹配。我把它做得很好,但是我似乎无法让它与多个变量一起工作。另外,我也想不出如何让它输出哪个变量与之匹配。
我以为这样的东西可以用来检查多个变量,但我错了:
$pos = strpos($mystring1, $mystring2, $findme);
如果有人能在这里提供帮助,这将是很棒的,这是我目前为一个变量工作的代码。
<?
if(isset($_POST["searchString"])) {
$mystring1 = 'how are you today';
$mystring2 = 'hello what is your name';
$findme = $_POST["searchString"];
$pos = strpos($mystring1, $findme);
if ($pos !== false) {
//found
} else {
//not found
}
}
?>
<html>
<body>
<form action="test.php" method="post">
<input type="text" name="searchString">
</form>
</body>
</html>
发布于 2017-09-04 06:16:49
你可以这样做。
<?
if(isset($_POST["searchString"])) {
$mystring1 = 'how are you today';
$mystring2 = 'hello what is your name';
$findme = $_POST["searchString"];
$pos = strpos($mystring1, $findme);
$pos2 = strpos($mystring2, $findme);
if ($pos !== false && $pos2 !== false) {
//found in both strings
} else if ($pos !== false || $pos2 !== false) {
//found in 1 of the 2 strings
} else {
//not found
}
if ($pos !== false) {
//found in string 1
}
if ($pos2 !== false) {
//found in string 2
}
}
?>
https://stackoverflow.com/questions/46038662
复制