Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Not wanted output

Posted on 2016-09-02
11
Medium Priority
?
61 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 59

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 59

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's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 59

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 59

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

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 …
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.
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…
The viewer will learn how to dynamically set the form action using jQuery.

715 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