Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Not wanted output

Posted on 2016-09-02
11
Medium Priority
?
64 Views
Last Modified: 2016-09-07
$find = '.5010.';

$dirname = 'renamed';
if(!is_dir($dirname))
    mkdir($dirname, 0777);

$directory_with_files = './';
$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($file, "r");
        $file_content = file_get_contents($file);
        
        if ($handle) {
            $header = '';
            $footer = '';
            $payor_blocks = array();
            
            // determine if file has more than one payor
            $payor_count = substr_count($file_content, 'N1');
            //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;
                
                while (($line = fgets($handle)) !== false) {
                    //look for the first payor block
                    if(strpos($line, 'N1') !== false || $block_start) {
                        $header_end = true; $block_start = true;
                        //see if the block finished
                        if(strpos($line, 'CAS') !== false) {
                            $block_start = false;
                            $payor_blocks[$count] = $line;
                            $count++;
                        }
                        $payor_blocks[$count] = $line;
                    } else {
                        //append to the header
                        if($header_end) {
                            $footer .= $line."\n";
                        } else {
                            $header .= $line."\n";
                        }
                    }
                }
                
                //get payor blocks and create a file foreach payor
                $new_files = array();
                foreach($payor_blocks as $block) {
                    $filename = '';
                    $new_files[] = array(
                        'name' => $filename,
                        'content' => $header."\n".$block."\n".$footer
                    );
                }
                
                //loop through new files and create them
                foreach($new_files as $new_file) {
                    //create file
                	$myfile = fopen("$file", "x");
                    //put contents in the file
                	//$txt = $new_file; 
                	fwrite($myfile, "$new_file");
                	//close the file
                	fclose($myfile);
                }
                
            } else {

            	// DO what u were doing in script #1
           while (($line = fgets($handle)) !== false)
            {
            	$refid = 'REF';
            	
            	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($file, $dirname.'/'.$filerenamed);
            		}
            		echo $refnumber . "\n";
            	}
            	}
            	 
            }
        }
        // DONE - close the file
        fclose($handle);
    }
}

Open in new window


I have the code above, it suppose to split the the file if it has more than one payor "N1" and cut the top and the body to put them in a new file. However , whenever I run this code, the file that contains more than one payor, deletes all its contents and just writes "Array", i dont see any issue however, any ideas?
0
Comment
Question by:Jasmine Ikhreishi
11 Comments
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 41781482
This is how you are filling $new_files
$new_files[] = array(
  'name' => $filename,
'content' => $header."\n".$block."\n".$footer
);

Open in new window

This is how you are writing to the file
foreach($new_files as $new_file) {
  //create file
  $myfile = fopen("$file", "x");

  //put contents in the file
  //$txt = $new_file; 
  fwrite($myfile, "$new_file");

  //close the file
  fclose($myfile);
}

Open in new window

$new_file is an array - you want the 'content' element of the array so you probably want to be doing this

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

Open in new window

0
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41781489
I changed the fwrite statement, however I still got the same output
"Array"
0
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 41781510
Please don't say you changed something - post the code of what you did we need to see what you are doing to make a comment.

The code I posted should work - but you have two variables that are almost identical in spelling
$new_files
$new_file

Chances are you used the wrong one.

Please post the code you say is not working.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41781514
$find = '.5010.';

$dirname = 'renamed';
if(!is_dir($dirname))
    mkdir($dirname, 0777);

$directory_with_files = './';
$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($file, "r");
        $file_content = file_get_contents($file);
        
        if ($handle) {
            $header = '';
            $footer = '';
            $payor_blocks = array();
            
            // determine if file has more than one payor
            $payor_count = substr_count($file_content, 'N1');
            //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;
                
                while (($line = fgets($handle)) !== false) {
                    //look for the first payor block
                    if(strpos($line, 'N1') !== false || $block_start) {
                        $header_end = true; $block_start = true;
                        //see if the block finished
                        if(strpos($line, 'CAS') !== false) {
                            $block_start = false;
                            $payor_blocks[$count] = $line;
                            $count++;
                        }
                        $payor_blocks[$count] = $line;
                    } else {
                        //append to the header
                        if($header_end) {
                            $footer .= $line."\n";
                        } else {
                            $header .= $line."\n";
                        }
                    }
                }
                
                //get payor blocks and create a file foreach payor
                $new_files = array();
                foreach($payor_blocks as $block) {
                    $filename = '';
                    $new_files[] = array(
                        'name' => $filename,
                        'content' => $header."\n".$block."\n".$footer
                    );
                }
                
                //loop through new files and create them
                foreach($new_files as $new_file) {
                    //create file
                	$myfile = fopen("$file", "x");
                    //put contents in the file
                	//$txt = $new_file; 
                	fwrite($myfile, "{$new_file['content']}");
                	//close the file
                	fclose($myfile);
                }
                
            } else {

            	// DO what u were doing in script #1
           while (($line = fgets($handle)) !== false)
            {
            	$refid = 'REF';
            	
            	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($file, $dirname.'/'.$filerenamed);
            		}
            		echo $refnumber . "\n";
            	}
            	}
            	 
            }
        }
        // DONE - close the file
        fclose($handle);
    }
}

Open in new window


This is the code that I edited and still didnt work. And in my foreach I did $new_files as $new_file, so Im not mistaken.
0
 
LVL 36

Expert Comment

by:Kimputer
ID: 41781523
line 79                      fwrite($myfile, "{$new_file['content']}");
to
line 79                      fwrite($myfile, "{$new_files['content']}");
0
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41781525
No in my foreach, $new_files as $new_file,so $new_file is correct
0
 
LVL 60

Assisted Solution

by:Julian Hansen
Julian Hansen earned 2000 total points
ID: 41781552
Are you sure it does not work?

Can you add the following on line 73 and post the output

echo "<pre>" . print_r($new_files, true) . "</pre>";

Open in new window

0
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41781569
Okay it worked when I did that, can I keep it?  Or is it just to check errors
0
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41781608
Now the $header and the $footer prints our, however $block doesnt, any ideas?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41781620
This expert suggested creating a Gigs project.
Julian may want to see the code, but I prefer to see your test data set with before and after examples, like what you posted in this question.  Once we have that we can almost always help you with the code.  Please show us your test data, thanks.

If you're new to PHP and want to learn the language, this article can help you get a good footing.  

You might also consider adding this request into E-E Gigs, where you can get detailed, hands-on professional help as your requirements evolve.
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 41781622
You can remove that line - it was only for debugging purposes.

As for why the block does not print - I don't know - I would approach whoever wrote the code to do the parsing of the file. This question was about the unwanted output.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question