[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Split one file into 2 and renaming the file name

Posted on 2016-08-30
2
Medium Priority
?
84 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
[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
2 Comments
 
LVL 36

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

649 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