php - 如果 strpos 不完全为假,如何返回真?

标签 php codeigniter strpos

我知道怎么做strpos有效,并且按预期工作,但在 if 中返回功能不是。

部分代码:

foreach ($socialstream as $key => $social) {
    //filtering in here
    $result= $this->search_objects($social);
    ....
}

我的函数 search_objects:

function search_objects($objects)
{
    $filters = array('word', 'test');
    foreach ($objects as $key => $value) {
        if (is_array($value) || is_object($value)) {
            $this->search_objects($value);
        } else {
            //look for faulty strings in value
            foreach ($filters as $filter) {
                if (!is_int($value) && strpos($value, $filter) !== false) {
                    return true;
                }
            }

        }
    }
    return false;
}

如果我打印出我的 $result , 我总是得到 false回来,而不是 trueif功能。我知道它到达了 if当大海捞针时,通过调试,它只是不返回它。

我错过了什么?

最佳答案

我认为你的问题与递归部分有关:

    if (is_array($value) || is_object($value)) {
        $this->search_objects($value);
    }

您可能想对返回值做一些事情。比如:if ($this->search_objects($value)) return true;

(话又说回来,我不确定你想要完成什么)

编辑:试试这个:

function search_objects($objects)
{
    $filters = array('word', 'test');
    foreach ($objects as $key => $value) {
        if (is_array($value) || is_object($value)) {
            if ($this->search_objects($value)) {
                return true;
            }
        } else {
            //look for faulty strings in value
            foreach ($filters as $filter) {
                if (!is_int($value) && strpos($value, $filter) !== false) {
                    return true;
                }
            }

        }
    }
    return false;
}

关于php - 如果 strpos 不完全为假,如何返回真?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20700685/

相关文章:

php查找包含关键字的字符串并获取标签之间的字符串

php 5 strpos() 返回 0 和 false 之间的区别?

php - 从 HTML 表格中插入单行数据

php - PHP 中单个循环中的多个生成器

php - 如何在 MySQLi 中从 DB 声明变量(过程)?

php - 如何使用 join 进行 where 查询

php - Codeigniter Controller 中的行数

php - 在 Laravel 更新查询中使用数组

php - 如何将值从 View 传递到 Controller

php - 在CSS中重叠图像