Solved

Split one file into 2 and renaming the file name

Posted on 2016-08-30
2
38 Views
Last Modified: 2016-09-07
For example of one file:
348657856**56985897568976
N1*PR*Jasmine sdgugu
gughyug
35647856834756
REF*2U*7248
gfhgfdugs
ghdugyuigy4576
4867586
N1*PR*John ~
eghufgguigr
4975876346
REF*2U*SMIL
tfgugrt47587
CAS495787656
5687564

Open in new window



I want to spilt N1*PR*Jasmine to be:

348657856**56985897568976
N1*PR*Jasmine sdgugu
gughyug
35647856834756
REF*2U*7248
gfhgfdugs
ghdugyuigy4576
4867586
CAS495787656
5687564

Open in new window



And
N1*PR*John ~:

348657856**56985897568976
N1*PR*John ~
eghufgguigr
4975876346
REF*2U*SMIL
tfgugrt47587
CAS495787656
5687564

Open in new window



However, some files just contain one name, I want them to just leave those files alone?

And for each file that had two, I want the filename like for example if the filename was for the one above :

98475789456.5010.4676897.edi
after splitting them, I want it to show as
98475789456.5010.4676897.edi
98475789456.5010.4676897_copy.edi

Open in new window


$files = './';
$dh  = opendir($files);



$i = 5010;
while(! feof($files)) {
	$contents = stream_get_line($dh,1000,"REF*2U*");
	file_put_contents('new_file.'.$i. $contents);
	
}

Open in new window




I have the code above, but it doesn't seem to work with me.
0
Comment
Question by:Jasmine Ikhreishi
2 Comments
 
LVL 35

Expert Comment

by:Kimputer
Comment Utility
This expert suggested creating a Gigs project.
7 lines of code won't help you if your explanation is already more than that.
You need logic for checking if splitting is needed, then actual splitting, then overwriting the original file and adding a copy, while line 1 in each file.
On top of that, seems you need to copy the last two lines to the first file as well? So yes, please start a gig
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
Please see the answer in your other question, code reproduced below for convenience:
https://www.experts-exchange.com/questions/28966489/Split-one-file-into-2-and-renaming-the-file-name.html

If you want to learn enough PHP to write this sort of programming yourself, please start here:
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html

I agree with Kimputer, probably best to move your requirements for application development over into E-E Gigs.

Here's the script from the other question.  I still do not understand the file naming expectations, but this shows how to separate the information according to the rules, as I understand them so far.
<?php // demo/temp_jasmine.php
/**
 * https://www.experts-exchange.com/questions/28966489/Split-one-file-into-2-and-renaming-the-file-name.html
 *
 * http://stackoverflow.com/questions/1430941/how-to-understand-an-edi-file
 *
    ORIGINAL FILE                 JASMINE                       JOHN
 1. 348657856**56985897568976     348657856**56985897568976     348657856**56985897568976
 2. N1*PR*Jasmine sdgugu          N1*PR*Jasmine sdgugu
 3. gughyug                       gughyug
 4. 35647856834756                35647856834756
 5. REF*2U*7248                   REF*2U*7248
 6. gfhgfdugs                     gfhgfdugs
 7. ghdugyuigy4576                ghdugyuigy4576
 8. 4867586                       4867586
 9. N1*PR*John ~                                                N1*PR*John ~
10. eghufgguigr                                                 eghufgguigr
11. 4975876346                                                  4975876346
12. REF*2U*SMIL                                                 REF*2U*SMIL
13. tfgugrt47587                                                tfgugrt47587
14. CAS495787656                  CAS495787656                  CAS495787656
15. 5687564                       5687564                       5687564

Distribution rules:
TOP Line goes to both files
N-1 and END Lines go to both files
Lines starting with N1*PR* begin a segment
Each segment goes into a file of its own, wrapped by TOP Line (1) and Lines N-1 (14) and END Line (15)
 */
error_reporting(E_ALL);
echo '<pre>';


// A CLASS TO REPRESENT THE EDI FILES
Class EdiFile
{
    public function __construct(array $a)
    {
        $a = array_values($a);
        $this->top = $a[0];
        $this->arr = [];
        $this->end = array_pop($a);
        $this->n_1 = array_pop($a);
    }
    public function addLine($x)
    {
        $this->arr[] = $x;
    }
    public function __toString()
    {
        $out = NULL;
        $out .= $this->top . PHP_EOL;
        $out .= implode(PHP_EOL, $this->arr) . PHP_EOL;
        $out .= $this->n_1 . PHP_EOL;
        $out .= $this->end . PHP_EOL;
        return $out;
    }
}


// MAKE AN ORIGINAL FILE, AS IF GOTTEN WITH PHP file()
$original_string = <<<EOD
348657856**56985897568976
N1*PR*Jasmine sdgugu
gughyug
35647856834756
REF*2U*7248
gfhgfdugs
ghdugyuigy4576
4867586
N1*PR*John ~
eghufgguigr
4975876346
REF*2U*SMIL
tfgugrt47587
CAS495787656
5687564
EOD;
$original_file = explode(PHP_EOL, $original_string);


// A SIGNAL STRING USED TO DECLOP THE ORIGINAL FILE INTO ITS PARTS
$signal = 'N1*PR*';

// COLLECT THE PARTIAL FILES HERE
$partials = [];

// START A NEW EDI PARTIAL FILE OBJECT
$edi = new EdiFile($original_file);


// REMOVE THE TWO END LINES FROM THE ORIGINAL
array_pop($original_file);
array_pop($original_file);

// REMOVE THE TOP LINE FROM THE ORIGINAL
array_shift($original_file);


// ITERATE OVER THE MODIFIED ORIGINAL FILE
foreach ($original_file as $str)
{
    // EACH SIGNAL STRING STARTS A NEW PARTIAL FILE
    if (strpos($str, $signal) === 0)
    {
        $key = $str;
        $partials[$key] = clone $edi;
    }
    $partials[$key]->addLine($str);
}

// SHOW THE WORK PRODUCT
foreach ($partials as $key => $file)
{
    echo PHP_EOL . "<h2>$key</h2>";
    echo PHP_EOL . $file;
    echo PHP_EOL;
}

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

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

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 count occurrences of each item in an array.

743 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

15 Experts available now in Live!

Get 1:1 Help Now