Solved

Not wanted output

Posted on 2016-09-02
11
53 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 55

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 55

Accepted Solution

by:
Julian Hansen earned 500 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 35

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 55

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 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 109

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 55

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

828 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