Solved

Split one file and make them into two

Posted on 2016-08-29
9
33 Views
Last Modified: 2016-09-04
//expression to be found in file name
$find = '.5010.';

//directory name
//we will store renamed files here
$dirname = '5010';
if(!is_dir($dirname))
    mkdir($dirname, 0777);

//read all files from a directory
//skip directories
$directory_with_files = './';
$dh  = opendir($directory_with_files);
$files = array();
while (false !== ($filename = readdir($dh)))
{
    if(in_array($filename, array('.', '..')) || is_dir($filename))
        continue;

    $files[] = $filename;
}

//iterate collected files
foreach($files as $file)
{
    //check if file name is matching $find
    if(stripos($file, $find) !== false)
    {

        //open file
        $handle = fopen($file, "r");
        if ($handle)
        {
            //read file, line by line
            while (($line = fgets($handle)) !== false)
            {

                //find REF line
                $refid = 'REF*2U*'; 
                if(stripos($line, $refid) !== false) 
                    { 
                    //glue refernce numbers 
                    //check if reference number is not empty 

                    $refnumber = str_replace(array($refid, '~'), array('', ''), $line);
                if($refnumber != '') 
                    { 
                $refnumber = '_'. $refnumber .'_'; 

                $filerenamed = str_replace($find, $refnumber, $file); 
                    copy($file, $dirname . '/' . $filerenamed); 
                        } 

                echo $refnumber . "\n"; 
                    }
            }

            //close file
            fclose($handle);
        }
    }
}

Open in new window


I have the code above, to read my files that contain ".5010." and replace it with the characters after REF*2U*. However in some files there are more than one REF*2U*, is there a code that would split them through a line called "N1*PR*" and output 2 files each with its own REF*2U* character?
0
Comment
Question by:Jasmine Ikhreishi
9 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41774785
This is where we could use the SSCCE.  We do not really need to see code that isn't working.  All we need to see are the data sets.  If you can show us the test data with the contents of before  and after  files, we can show you the code to bridge the gap.
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41774880
Can you give some examples of the input and output. What does a sample file look like, what is its name what output does the script produce currently.
What does the file look like for the case you want to cater for - what does the output from that look like?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41774889
Now that I've had a look at your other question and its file (at least that's what I think I'm looking at) I wonder if it might not be a good idea to step back from this implementation and take a higher-level look at the problem.  I have been in software development for years and I have never seen a design pattern that looks quite like this.  Maybe if you tell us about the task in plain, non-technical language, we can suggest a more mainstream solution.  Questions that come to mind are: Where does this data come from?  How does it get used?  If we can understand that we can probably give you a much simpler design!
0
 
LVL 42

Expert Comment

by:zephyr_hex (Megan)
ID: 41774908
This looks like EDI data.  REF and N1 are both EDI segment identifiers.  And 5010 is an EDI format specification.

So my guess is that the OP is attempting to parse EDI transactions.

There's a pretty decent python based parser I've used for this called Bots, although I'm not sure if the OP is looking for an open source solution that's not based in PHP.

Parsing EDI data is quite tricky because of the various permutations that can occur.  Accounting for more than on REF segment is just the tip of the iceberg.
1
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41776158
I re-asked it in a better way in another question, please take a look!
0
 
LVL 1

Accepted Solution

by:
Jasmine Ikhreishi earned 0 total points
ID: 41776173
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41776215
I'll take a look at your other question, but in the future, you can still use a single question -- just provide the additional information that we ask for, and we can continue the discussion "under one roof."
1
 
LVL 1

Author Closing Comment

by:Jasmine Ikhreishi
ID: 41783533
I re-asked it in a better manner
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

895 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

17 Experts available now in Live!

Get 1:1 Help Now