Search code examples
phpphpexcelstrpos

strpos returns true when using clearly different strings


I'm taking data from an excel using phpexcel, like this:

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

number is clearly a number, okay? so, after that I want to know if $number exists on the word $elem:

if(strpos($elem,$number) !== false) //está?
                {
                    $answer = true;
                }

the problem is that when I test it with this data, the $answer is true, and it shouldn't be:

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

So... what's wrong here?


PD: I'm going to post the entire code so you can see it, If I try to do (string)$number then the code crashes, it exceeds time execution.... (using cakephp)

The important thing is located at the function SearchPhoto... you will see the strpos there...

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

Solution

  • I ended up using preg_match to solve my problem... I still don't know what's wrong with using strpos... because it works on all the sites I made expect this particular case!

    In case anyone wondering what's the exact solution:

    $text = $B;
    $pattern = '/'.$A.'/';
    
    preg_match($pattern,$text,$matches);
    
    if(isset($matches[0]))
        $answer = true;