strpos returns 使用明显不同的字符串时为真

strpos returns true when using clearly different strings

我正在使用 phpexcel 从 excel 获取数据,如下所示:

$number = $objPHPExcel->getActiveSheet()->getCell('A3')->getValue();

number 明明是个数字好吗?所以,之后我想知道 $elem:

这个词是否存在 $number
if(strpos($elem,$number) !== false) //está?
                {
                    $answer = true;
                }

问题是当我用这个数据测试它时,$answer 是真的,它不应该是:

$number = 11001456
$elem = '10001033.jpg'

所以...这是怎么回事?


PD:我要 post 整个代码,这样你就可以看到它,如果我尝试执行 (string)$number 然后代码崩溃,它超过了执行时间....(使用蛋糕php)

重要的是位于 SearchPhoto 函数中...您会在那里看到 strpos...

public function admin_load() //esto sirve para cargar un excel...
    {

        if($this->request->is('post'))
        {
            $data = $this->request->data;

            error_reporting(E_ALL);
            ini_set('display_errors', TRUE);
            ini_set('display_startup_errors', TRUE);

            define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

            date_default_timezone_set('Europe/London');

            /** Include PHPExcel_IOFactory */
            require_once WWW_ROOT . '/excelWorker/Classes/PHPExcel/IOFactory.php';

            echo date('H:i:s') , " Load from Excel2007 file" , EOL;
            $callStartTime = microtime(true);

            $objPHPExcel = PHPExcel_IOFactory::load($data['People']['excel']['tmp_name']);

            $dir = WWW_ROOT . "img/photos";
            $files = scandir($dir);

            $batchPeople = array();

            for($i = 2; $i <= $data['People']['num']; $i++)
            {
                $batchPeople[$i-2]['People']['fullname'] = $objPHPExcel->getActiveSheet()->getCell('B'.$i)->getValue();
                $batchPeople[$i-2]['People']['floor'] = $objPHPExcel->getActiveSheet()->getCell('C'.$i)->getValue();
                $batchPeople[$i-2]['People']['country'] = $objPHPExcel->getActiveSheet()->getCell('D'.$i)->getValue();
                $batchPeople[$i-2]['People']['day'] = $objPHPExcel->getActiveSheet()->getCell('F'.$i)->getValue();
                $batchPeople[$i-2]['People']['month'] = $objPHPExcel->getActiveSheet()->getCell('G'.$i)->getValue();
                $batchPeople[$i-2]['People']['photo'] = $this->SearchPhoto($objPHPExcel->getActiveSheet()->getCell('A'.$i)->getValue(),$files);
            }

    //      $this->People->saveMany($batchPeople);

        }

    }

    function SearchPhoto($number, $array)
    {
        $answer = '';
        $getOut = false;

    foreach($array as $elem)
    {
        if(strcmp($elem,'.') != 0 && strcmp($elem,'..') != 0)
            if(strpos($elem,$number) !== false) //está?
            {
                echo 'coinciden--> '. $number . ',' . $elem;
                echo '<br>';
                $answer = $elem;
                $getOut = true;
            }
            if($getOut)
                break;
    }

    return $answer;
}

这应该适合你:

(顺便说一句:您在代码中使用 $number 而不是 $num

<?php


    $num = 11001456;
    $elem = "10001033.jpg";

    if(strpos($elem, (string)$num) !== false) {
        echo "yes";
    }

?>

有关 strpos() 的更多信息,请查看手册:http://php.net/manual/en/function.strpos.php

引自那里:

needle: If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.

echo chr($number);        // p
echo strpos($elem, 'p');  // 10 (which is p in $elem)

我最终使用 preg_match 来解决我的问题...我仍然不知道使用 strpos 有什么问题...因为它适用于我期望这个特殊情况的所有网站!

如果有人想知道确切的解决方案是什么:

$text = $B;
$pattern = '/'.$A.'/';

preg_match($pattern,$text,$matches);

if(isset($matches[0]))
    $answer = true;