Link to home
Start Free TrialLog in
Avatar of Jazzy 1012
Jazzy 1012

asked on

Split one file into 2 and renaming the file name

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

$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.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

I'll try to help, but I strongly suggest you consider posting this in E-E Gigs.  It's not really a question with an answer, so much as a requirement for application program development, and for that you will be much better off to work with a professional developer.
This should help you get started.  I do not understand the rules for the file names, but you can write each of the EdiFile objects into its own file using any name that makes sense for your application.
https://iconoun.com/demo/temp_jasmine.php
<?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

Outputs:
N1*PR*Jasmine sdgugu


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


N1*PR*John ~


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

Open in new window

This should also work correctly with a file having only one embedded signal.
Avatar of Jazzy 1012
Jazzy 1012

ASKER

Okay, thanks but if there is more than one jasmine, can it keep the file as it is, Like if the name is different thats when it'll split?
Also is there a way by using array_splice that I can make the removal of the last two lines dynamically, because some are 5 when some are two (like the example)
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial