-1

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;
}
WhiteFloater
  • 137
  • 1
  • 1
  • 10

2 Answers2

1

This should work for you:

(BTW: You use $number in the code and not $num)

<?php


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

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

?>

For more information about strpos() look into the manual: http://php.net/manual/en/function.strpos.php

And a quote from there:

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)
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • @WhiteFloater Did you tested the code with your provided test data? (`$num = 11001456; $elem = "10001033.jpg";`) – Rizier123 Jan 16 '15 at 20:58
  • @WhiteFloater And yes it doesn't! See this: http://ideone.com/1icQRa Do you see any yes on the screen? – Rizier123 Jan 19 '15 at 12:41
  • @WhiteFloater I gave you even a Demo and this shows you clearly that this works! – Rizier123 Jan 19 '15 at 12:59
  • Yes I see it, but obviously there's something in my code that is not working, and I don't have idea what it is... I mean... I posted the entire code here but... nothing.... – WhiteFloater Jan 19 '15 at 13:00
  • @WhiteFloater Make sure that you even get to this if statement! And if not then you have somewhere else a logic error. OR you just don't show the real code, but the `strpos` works with my answer! – Rizier123 Jan 19 '15 at 13:02
0

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;
WhiteFloater
  • 137
  • 1
  • 1
  • 10