Solved

Not wanted output

Posted on 2016-09-02
11
41 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 51

Expert Comment

by:Julian Hansen
Comment Utility
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
Comment Utility
I changed the fwrite statement, however I still got the same output
"Array"
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
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
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
Comment Utility
$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 35

Expert Comment

by:Kimputer
Comment Utility
line 79                      fwrite($myfile, "{$new_file['content']}");
to
line 79                      fwrite($myfile, "{$new_files['content']}");
0
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:Jasmine Ikhreishi
Comment Utility
No in my foreach, $new_files as $new_file,so $new_file is correct
0
 
LVL 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Now the $header and the $footer prints our, however $block doesnt, any ideas?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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 51

Expert Comment

by:Julian Hansen
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now