Solved

Not wanted output

Posted on 2016-09-02
11
47 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 52

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 52

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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 52

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 108

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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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 …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

919 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

12 Experts available now in Live!

Get 1:1 Help Now