dtzd65908 2010-11-29 19:17
浏览 87
已采纳

PHP array_search无效

Hey guys and girls, i'm stumped. Trying to get array_search to work with this script.

 <?php 

$dir = '/var/www/html/pay.group.com/upload';
$i = 0;


if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {

            if ($file != "." && $file != ".."){
            //convert files from pdf to text
            exec("pdftotext /var/www/html/pay.group.com/upload/" . $file . " /var/www/html/tmp/converted/" . $file);
            //create array from text files
            $current_array = file("/var/www/html/tmp/converted/" . $file) or die ("<br/>**cannot find file to create array**"); 
            //search array
            echo array_search('EMPLOYEE NO. ',$current_array);      
            $i++;

            echo var_dump($current_array);
            }
        }
        closedir($dh);

        echo "$i files processed"; 
    }
}


?>

I get nothing from the array_search and I can't figure out why, its driving me mad.

Here is a relevant part of the var_dump that is working correctly.

"NON NEGOTIABLE " [28]=> string(5) "9871 " [29]=> string(13) "EMPLOYEE NO. " [30]=> string(1) " " [31]=> string(3) "01 " [32]=> string(6) "SHIFT " [33]=> string(1) " " [34]=> string(4) "MIC " [35]=> string(19) "LOCATION HRS/UNITS "

Is there something I am doing wrong? The string for the array search is exactly the same as it is in the actual array so I can't figure out why its not returning an array index for me.

Using the pre tag, this is what I get.

  [27]=>
  string(15) "NON NEGOTIABLE
"
  [28]=>
  string(5) "9871
"
  [29]=>
  string(13) "EMPLOYEE NO.
"
  [30]=>
  string(1) "
"
  [31]=>
  string(3) "01
"
  [32]=>
  string(6) "SHIFT
"
  [33]=>
  string(1) "
"
  [34]=>
  string(4) "MIC
"
  [35]=>
  string(19) "LOCATION HRS/UNITS
"
  [36]=>
  string(1) "
"
  • 写回答

4条回答 默认 最新

  • duandeng2011 2010-11-29 19:29
    关注

    The lines in the uploaded file are on separate lines. file() function leaves the newline characters attached to the array items and this the reason why search does not work.

    You can strip newlines from all array items like this

    array_walk($current_array, 'trim');
    

    After that your search should work.

    Or, as KingCrunch said, use

    file("/var/www/html/tmp/converted/" . $file, FILE_IGNORE_NEW_LINES);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助