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) "
"
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
After that your search should work.
Or, as KingCrunch said, use