我已经阅读了有关如何过滤某些针头的字符串的所有帖子,我认为stripos()
将会完成这项工作,但是,例如找到第一个针时,该函数不会返回true它仅使用我的数组的第二个值过滤haystack。
示例:
$String = 'IWantToSolveThisProblem!!';
$needle = array('want', 'solve');
foreach ($needle as $value) {
$res = stripos($String, $value,0);
if($res !==false){
echo 'found';
}
else {
echo 'not found'; }}
在上面的示例中,输出将回显'Found',因为两个值都出现在我的字符串中。
问题是它只使用我的数组的最后一个值来循环。如果第一个值存在于String中而不是第二个值,则它将返回false。 我不想运行多个if语句
答案 0 :(得分: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";
}
}
答案 1 :(得分: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";
}
答案 2 :(得分: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
所以它似乎对我有用......你可能会检查拼写错误...... ??!
答案 3 :(得分: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-loop“转换”为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条件(数组中每个元素一个)
答案 4 :(得分: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;
}
答案 5 :(得分:-1)
您收到“未找到”,因为当$ value变为“解决”时,$String = 'IWantAnAnswer'.