需要按顺序运行两次才能完全正常工作

时间:2016-09-10 11:45:07

标签: php

$find = '.5010.';
$directory_with_files = './'.date('m-d-Y');
$dh  = opendir($directory_with_files);
$files = array();
while (false !== ($filename = readdir($dh)))
{
if(in_array($filename, array('.', '..')) || is_dir($filename))
continue;

$files[] = $filename;}

foreach($files as $file){
//find only 5010 files
if(stripos($file, $find) !== false){
// open the 5010 file
$handle = fopen(date('m-d-Y').'/'.$file, "r");
$file_content = file_get_contents(date('m-d-Y').'/'.$file);
$handle2 = fopen(date('m-d-Y').'/'.$file, "r");
$file_content2 = file_get_contents(date('m-d-Y').'/'.$file);

if ($handle) {
$header = '';
$name = '';
$footer = '';
$payor_blocks = array();


// determine if file has more than one payor
$payor_count = substr_count($file_content, 'N1*PR*');
//if the file has more than one payor
if($payor_count > 1) {
//read the file line by line
$header_end = false;
$block_start = false;
$count = 1;


if($handle2){
$line_number = 0;
$line_stop= array();
while (($line1 = fgets($handle2)) !== false) {

$line_number++;

if(strpos($line1, 'CAS') !==false){

$line_stop[] = $line_number;}}
$footer_line = count($line_stop)-2;
$footer_line = $line_stop[$footer_line];              
$line_number = 0; }

//look for occurances of CAS and what line each on is on

while (($line = fgets($handle)) !== false) {
$line_number++;

//look for the first payor block
if(strpos($line, 'N1*PR*') !== false || $block_start) {
$header_end = true; $block_start = true;
if(strpos($line, 'N1*PR*') !== false) {
$count++;
}  

//see if the block finished
if($line_number == $footer_line) {
$block_start = false;
$payor_blocks[$count] .= $line;
$count++; }
$payor_blocks[$count] .= $line;} 
else {
if($header_end) {
$footer .= $line."\n";  }
else {
$header .= $line."\n";}}

$refid = 'REF*2U*';

if(stripos($line, $refid) !== false)
{
$refnumber = str_replace(array($refid, '~'), array('', ''), $line);
$refnumber = trim($refnumber);

if($refnumber != '')
{
$refnumber = '_'.$refnumber.'_';
$filerenamed = str_replace($find, $refnumber,$file);
copy('./'.date('m-d-Y').'/'.$file, './'.date('m-d-Y').'/'. $filerenamed);
}
echo $refnumber . "\n";

}
}
//get payor blocks and create a file foreach payor
$new_files = array();
foreach($payor_blocks as $block) {
$filename = date('m-d-Y').'/'.$file . "_" . $count;
$count++;
$new_files[] = array(
'name' => $filename,
'content' => $header."\n".$block."\n".$footer   

);

}

foreach($new_files as $new_file) {

$myfile = fopen($new_file['name'], "w");

fwrite($myfile, $new_file['content']);  

fclose($myfile);
}

} 

else{

while (($line = fgets($handle)) !== false)
{
$refid = 'REF*2U*';

if(stripos($line, $refid) !== false)
{
$refnumber = str_replace(array($refid, '~'), array('', ''), $line);
$refnumber = trim($refnumber);

if($refnumber != '')
{
$refnumber = '_'.$refnumber.'_';
$filerenamed = str_replace($find, $refnumber,$file);
copy('./'.date('m-d-Y').'/'.$file, './'.date('m-d-Y').'/'. $filerenamed);
}
echo $refnumber . "\n";

}

}
}
}

}
// DONE - close the file
fclose($handle);
}

foreach($files as $fiftyfile){
if(stripos($fiftyfile, $find) !== false){

$handle3 = fopen(date('m-d-Y').'/'.$fiftyfile, "r");
$file_content3 = file_get_contents(date('m-d-Y').'/'.$fiftyfile);

if ($handle3) {
if(unlink('./'.date('m-d-Y').'/'.$fiftyfile))
{
echo "file named $fiftyfile has been deleted successfully";
}
else
{
echo "file is not deleted";
}
}
}

}

我的目录中有一些文件,其文件名包含“3256.5010.548674.23a”在此代码中,它打开文件并搜索是否有多个“N1 * PR *”并且是否要将它们拆分为单独的文件。最后改变“.5010”。到REF号码,就像“ 8743 ”。然后它删除所有文件“.5010”。并将其余部分合并到一个文档中。它工作正常但是,当我第一次运行它时它会拆分并重命名,但只删除第一个文件而不是所有的“.5010”。 (不是那些被拆分的),然后当我再次运行它之后,它会删除所有内容,但重命名旧的,因为它会通过“else语句”进行重命名。我怎么能用删除解决问题?

0 个答案:

没有答案