简体   繁体   English

PHP-使用Stripos()过滤字符串

[英]PHP - Using Stripos() to filter a String

I have read all thread about how to filter a string for certain needles and I think stripos() is what will do the job, however, the function does not return true when for example the first needle is found as if it is filtering the haystack using only the second value of my array. 我已经阅读了有关如何过滤某些针的字符串的所有线程,并且我认为stripos()可以完成此工作,但是,例如,当找到第一个针时,该函数就不会返回true,就好像它在过滤干草堆一样仅使用数组的第二个值。

Example : 范例:

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

foreach ($needle as $value) {

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

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

In the example above the output will echo 'Found' because both values are present in my string. 在上面的示例中,输出将回显“ Found”,因为这两个值都存在于我的字符串中。

The problem is that it is only using the last value of my array to loop. 问题在于它仅使用数组的最后一个值来循环。 If the first value is present within the String and not the second it will return as false. 如果第一个值出现在String中,而不是第二个,它将返回false。 I dont want to run multiple if statements 我不想运行多个if语句

$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'; }}

This doesn't make sense now... It's not that it's only comparing the last element. 现在这没有意义了……不是说它只是比较最后一个元素。 It just isn't comparing the first. 只是没有比较第一个。 See this example... 看到这个例子...

$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".

It all makes sense now... From the docs: 现在一切都变得有意义了...来自文档:

"This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function." “此函数可能返回布尔值FALSE,但也可能返回非布尔值,该值的值为FALSE。有关更多信息,请阅读布尔值部分。使用===运算符测试此函数的返回值。”

So, changing if(stripos($haystack,$needle,0)) to if(stripos($haystack,$needle,0) !== False) fixes the logic. 因此,将 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"; 
    }
}

You should do: 你应该做:

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";
}

I tested with this code: 我用以下代码进行了测试:

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

and it outputs: 它输出:

$php testme.php

want found
solve not found

So it seems to work for me here... you might check for typos maybe..??! 所以这似乎对我有用...您可能会检查错别字.. ?? !!

<?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'; }}
?>

Output: foundfound 输出:foundfound

If you "convert" your foreach-loop to if statements it would be the same as: 如果将“ 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";
}

Output: foundfound 输出:foundfound

So the foreach - loop does several if-conditions (one for each element in the array) 因此,foreach-循环会执行多个if条件(数组中的每个元素一个)

I've interpreted your question as a search to find a function that returns true when all needles are present in the given haystack, and here's a function that does just that... 我已将您的问题解释为一种搜索,以找到一个当所有干草堆都存在于给定干草堆中时返回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

It would be trivial to change this function to return true if any of the needles are found, instead of all needles. 如果找到任何一个针而不是所有针,则更改此函数以返回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;
}

您将得到“未找到”,因为当$ value变为“ solve”时,在$String = 'IWantAnAnswer'.找不到该$String = 'IWantAnAnswer'.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM