我正在使用phpexcel从excel中获取数据,如下所示:
$number = $objPHPExcel->getActiveSheet()->getCell('A3')->getValue();
数字显然是一个数字,好吗?所以,之后我想知道$ elem:这个词是否存在$ number
if(strpos($elem,$number) !== false) //está?
{
$answer = true;
}
问题是当我用这些数据测试时,$ answer是真的,它不应该是:
$number = 11001456
$elem = '10001033.jpg'
那么......这里有什么问题?
重要的是位于功能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;
}
答案 0 :(得分:1)
这应该适合你:
(顺便说一句:您在代码中使用$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不是字符串,则将其转换为整数并应用为字符的序数值。
echo chr($number); // p
echo strpos($elem, 'p'); // 10 (which is p in $elem)
答案 1 :(得分:0)
我最终使用preg_match来解决我的问题...我仍然不知道使用strpos有什么问题...因为它适用于我所期望的所有网站! / p>
如果有人想知道什么是确切的解决方案:
$text = $B;
$pattern = '/'.$A.'/';
preg_match($pattern,$text,$matches);
if(isset($matches[0]))
$answer = true;