Solved

Split one file into 2 and renaming the file name

Posted on 2016-08-30
5
33 Views
Last Modified: 2016-09-06
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.
0
Comment
Question by:Jasmine Ikhreishi
  • 3
  • 2
5 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41776287
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.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41776355
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.
0
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41777886
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?
0
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41777941
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)
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41778009
There are both short and long answers to both of your recent questions.  Short answers: Yes.

Longer answers: You need to hire a professional application developer to help you write this code.  The process needs to start with a full exposition of the input data and the expected outputs.  You need to create a test data set.  The exercises here are piecemeal and might be steps toward developing a generalized application, but as I wrote above, these are not questions with answers; they are requirements for application development, and they seem to be evolving even as we try to help you answer them.

E-E is pretty good for getting your questions answered, or even getting simple code examples, but we can't develop your applications for free.  Please consider using E-E Gigs.  It will be the fastest path to success because you can hire someone who already has all the knowledge needed to create the applications you need.

If you want to become a developer, and learn all the things a professional already knows, that's a reasonable goal, too.  But you should expect it to take some time, with structured, progressive learning and practice.  Most of us go to college to study these things over several semesters, if not years.  So like most things, there is a trade-off to be made between time and money!

Some good "getting started" resources for new PHP developers are shown in this article.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html

Best of luck with your project! ~Ray
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

821 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