Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Split one file into 2 and renaming the file name

Posted on 2016-08-30
2
Medium Priority
?
87 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 37

Expert Comment

by:Kimputer
ID: 41776306
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 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 41776367
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month21 days, 2 hours left to enroll

810 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