1

我已经阅读了有关如何为某些针过滤字符串的所有线程,我认为stripos()这将完成这项工作,但是,例如,当找到第一根针时,该函数不会返回 true,就好像它仅使用过滤干草堆一样我的数组的第二个值。

例子 :

$String = 'IWantToSolveThisProblem!!';
$needle = array('want', 'solve');

foreach ($needle as $value) {

$res = stripos($String, $value,0);
if($res !==false){

echo 'found';
}
else {
echo 'not found'; }}

在上面的示例中,输出将回显“找到”,因为这两个值都存在于我的字符串中。

问题是它只使用我的数组的最后一个值来循环。如果第一个值存在于字符串中而不是第二个值,它将返回 false。我不想运行多个 if 语句

4

6 回答 6

0

我用这段代码测试过:

<?php
$str = "IWantToSolveThisProblem!";
$str2 = 'IWantAnAnswer';
$needles = array('want', 'solve');

foreach ($needles as $needle){
        $res = stripos($str2, $needle, 0);
        if ($res !== false){
                echo "$needle found\n";
        }
        else {
                echo "$needle not found\n";
        }
}
echo "\n";
?>

它输出:

$php testme.php

want found
solve not found

所以它似乎在这里为我工作......你可能会检查错别字......??!

于 2013-04-22T19:48:45.610 回答
0

我已将您的问题解释为寻找一个函数,当所有针都存在于给定的干草堆中时,该函数返回 true,而这里有一个函数可以做到这一点......

/**
 * Search the haystack for all of the given needles, returning true only if 
 * they are all present.
 * 
 * @param  string  $haystack  String to search
 * @param  array   $needles   Needles to look for
 * @return boolean            
 */
function stripos_array($haystack, array $needles)
{
    foreach ($needles as $needle)
    {
        if (stripos($haystack, $needle) === false) return false;
    }
    return true;
}

// Test the function

$string = 'IWantToSolveThisProblem!!';

$search = array('want', 'solve');
$found = stripos_array($string, $search);
echo $found ? 'found' : 'not found', "\n"; // found

$search = array('want', 'cheese');
$found = stripos_array($string, $search);
echo $found ? 'found' : 'not found', "\n"; // not found

$search = array('cheese', 'problem');
$found = stripos_array($string, $search);
echo $found ? 'found' : 'not found', "\n"; // not found

如果找到任何针而不是所有针,则将此函数更改为返回 true 将是微不足道的。

/**
 * Search the haystack for any of the given needles, returning true if any of
 * them are present.
 * 
 * @param  string  $haystack  String to search
 * @param  array   $needles   Needles to look for
 * @return boolean            
 */
function stripos_array($haystack, array $needles)
{
    foreach ($needles as $needle)
    {
        if (stripos($haystack, $needle) !== false) return true;
    }
    return false;
}
于 2013-04-22T22:31:47.307 回答
0
$String = 'IWantToSolveThisProblem!!'; //you were missing this semicolon
$needle = array('want', 'solve'); //no spaces before the array (not needed, I just like nospaces :P)

foreach($needle as $value) {

$res = stripos($String, $value,0);
if($res !==false){

echo 'found';
}
else {
echo 'not found'; }}

现在这没有意义......这并不是说它只是比较最后一个元素。它只是没有比较第一个。看这个例子...

$haystack = "string to be tested";
$needles  = array("string","to","pineapple","tested");
foreach($needles as $needle){
    if(stripos($haystack,$needle,0)){
        echo "The word \"$needle\" was found in the string \"$haystack\".";
    }
    else{
        echo "The word \"$needle\" was NOT found in the string \"$haystack\".";
    }
}

Expected Output:
The word "string" was found in the string "string to be tested".
The word "to" was found in the string "string to be tested".
The word "pineapple" was NOT found in the string "string to be tested".
The word "tested" was found in the string "string to be tested".

Actual Output:
The word "string" was NOT found in the string "string to be tested".
The word "to" was found in the string "string to be tested".
The word "pineapple" was NOT found in the string "string to be tested".
The word "tested" was found in the string "string to be tested".

现在一切都说得通了......来自文档:

“此函数可能返回布尔值 FALSE,但也可能返回非布尔值,其计算结果为 FALSE。有关更多信息,请阅读布尔值部分。使用 === 运算符测试此函数的返回值。”

因此,更改 if(stripos($haystack,$needle,0))if(stripos($haystack,$needle,0) !== False) 修复逻辑。

$String = 'IWantToSolveThisProblem!!'; 
$needle = array('want', 'solve', 42); 

foreach($needle as $value) {

if(stripos($String, $value,0) !== FALSE){
    echo "found \"$value\" in \"$String \"";
}
else {
    echo "$value not found"; 
    }
}
于 2013-04-22T19:45:25.623 回答
0

你应该做:

if(stripos($value,$array[0]) !== false){ //check for the first element 
   echo "found want";
}
if(stripos($value,$array[1]) !== false){ //check for the second element
   echo "found solve";
}
于 2013-04-22T19:46:29.493 回答
0
<?php
$String = 'IWantToSolveThisProblem!!';
$needle = array('want', 'solve');

foreach ($needle as $value) {

$res = stripos($String, $value,0);
if($res !==false){

echo 'found';
}
else {
echo 'not found'; }}
?>

输出:foundfound

如果您将 foreach 循环“转换”为 if 语句,它将与以下内容相同:

if(stripos($String,$needle[0]) !== false){ //check for the first element 
   echo "found";
}
if(stripos($String,$needle[1]) !== false){ //check for the second element
   echo "found";
}

输出:foundfound

所以 foreach - 循环执行几个 if 条件(数组中的每个元素一个)

于 2013-04-22T21:49:52.570 回答
-1

您得到“未找到”,因为当 $value 变为“解决”时,在$String = 'IWantAnAnswer'.

于 2013-04-22T20:21:43.663 回答