• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

HOW DO I ORGANIZE AND DISPLAY TXT VIA PHP

I have a large / unorganized text file that is created by a system.  Attached are two files.  One called beginningfile.txt and endresult.txt.  What I would like is organization in this file and displayed on a webpage via PHP.  The ENDRESULT is just an example.  Whatever is easiest will also be fine.  Essentially, instead of searching the text file or printing it and looking for '123456' and '456789' as UNIQUE ID's for the rest of the respective data and manually match them up, I would like for the PHP to search for 123456 or 456789 in this case and put them all together.  In one instance on the file it will say 02123456, in another it will say 04123456 and yet another it will only say 123456 ((SECTIONIII) column I, if this piece of the text was in a csv file).

Bottom line: there are three separate pieces of data that should be together, but aren't.  (for convenience, I have labeled section I,II,III although the titles don't exist in the real file).  I want to get these three separated values for the same UNIQUE ID gathered together somehow.  Alternative layouts are welcome.  Many thanks!!!

Again, of course ENDRESULT wouldn't be an endresult.txt, rather just HTML on Firefox or other browser.
BEGINNINGFILE.txt
ENDRESULT.txt
0
weklica
Asked:
weklica
  • 8
  • 6
1 Solution
 
weklicaAuthor Commented:
Thanks.  I will look into these and see what I can come up with.  if anybody has code examples they would like to post --> keep in mind, the 123456 and 456879 are just examples.  There will be thousands of such numbers in the file.
0
 
NerdsOfTechTechnology ScientistCommented:
When you say thousands of numbers in the file are they separated by sections; if so, how are they other sections "linked"?

Thanks
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
weklicaAuthor Commented:
The file would look like this the following only a hundred times longer.  Just remember to think of 02123456 and 02456789 as examples only.  This first section blocked off below would likely have 200+ various numbers (all preceding with 02).

 ---------------------                                                                                                                                                                                                                                                                    
02123456        test1       test1    321321                                                                                                                                                                                                                                                                                
02456789        test2         test2   654654                                                                                                                                                                                                                                                                                
 ---------------------                                                                                                                                                                                                                                                           04123456       123456789121 1234567     26    001  0002172NPBAR                                         V11.22 V33.6                                                                                  7654321   98765432                                                                                                        AB1

04456789      123456789121 1234567     25    002  0002172ASDF                                         V11.22 V33.6                                                                                  7654321   98765432                                                                                                        AB2

04456789      123456789121 1234567     25    002  0002172ASDF                                         V11.22 V33.6                                                                                  7654321   98765432                                                                                                        AB3

TESTER2,TEST2,B,1234,ASDFF,DR,,ASDFASDF,EE,12345,123456,98798,ETC,ETC,ETC,ETC,ETC,ETC,ETC,ETC,ETC,ETC,ETC,ETC
TESTER,TEST,B,1234,ASDFF,DR,,ASDFASDF,EE,12345,456789,98798,ETC,ETC,ETC,ETC,ETC,ETC,ETC,ETC,ETC,ETC,ETC,ETC
TESTER,TEST,B,1234,ASDFF,DR,,ASDFASDF,EE,12345,123456,98798,ETC,ETC,ETC,ETC,ETC,ETC,ETC,ETC,ETC,ETC,ETC,ETC
0
 
NerdsOfTechTechnology ScientistCommented:
Okay I see 3 sections:
     
*SECTIONI*
*SECTIONII*
*SECTIONIII*


Does it look like this:
*SECTIONI*
#
# x 2
*SECTIONII*
#
# x 2
*SECTIONIII*
#
#
# x 1000
#
#

Like this:

*SECTIONI*
#
# x 1000
#
*SECTIONII*
#
# x 1000
#
*SECTIONIII*
#
# x 1000
#

Basically what I am trying to get at is: Is any one section BIGGER than the others?
0
 
weklicaAuthor Commented:
Yes,

Section II may have the same number a couple of times.  Section I will only have the number once, and Section III may have double the numbers.

Really, I only need the Matches for Section I displayed with its corresponding Section II and Section III.  

If Section III has no corresponding Section II or Section I, I don't need it displayed.  You will notice in the 'endresult.txt' file, Section I is the first line,  Section II would be the next four (or 8 or however many, depending if it shows up more than once or twice), and Section three would follow).  I am not overly concerned about how it is organized, just that they are grouped together a bit better.  Attached is a screencast of the general idea.....

I can type in the numbers I am concerned about, and the Console on MAC will display all chunks of the file which contain the given string, that is essentially what I am after here....


http://screencast.com/t/urAtCxogih

0
 
NerdsOfTechTechnology ScientistCommented:
Do you have control over the output format of this file?
0
 
weklicaAuthor Commented:
It is txt format only although I could script it out to become csv if necessary.
0
 
NerdsOfTechTechnology ScientistCommented:
First lets get rid of all of that whitespace!

rename your input file to bfile.txt

=NerdsOfTech
<?php
$handle = fopen('bfile.txt', 'r'); // open file
echo '<pre>';
while (!feof($handle)) { // if not End Of File then loop
 $data = fgets($handle); // read line
 $data = trim($data); // trim spaces from front and back
 $data=preg_replace('/\s+/',' ',$data); // regex: trim all whitespace to 1 space
 echo "$data\n"; // output line
}
echo '</pre>';
?>

Open in new window

regex-trim-all-spaces-plustrim.png
0
 
NerdsOfTechTechnology ScientistCommented:
Almost forgot the fclose :)
<?php
$handle = fopen('bfile.txt', 'r'); // open file
echo '<pre>';
while (!feof($handle)) { // if not End Of File then loop
 $data = fgets($handle); // read line
 $data = trim($data); // trim spaces from front and back
 $data=preg_replace('/\s+/',' ',$data); // regex: trim all whitespace to 1 space
 echo "$data\n"; // output line
}
fclose($handle);
echo '</pre>';
?>

Open in new window

0
 
NerdsOfTechTechnology ScientistCommented:
Next step remove newlines
<?php
$handle = fopen('bfile.txt', 'r');
echo '<pre>';
while (!feof($handle)) {
 $data = fgets($handle);
 $data = trim($data);
 $data=preg_replace('/\s+/',' ',$data);
 if ($data != ' ' && $data != '')
 {
  echo "$data\n";
 }
}
fclose($handle);
echo '</pre>';
?>

Open in new window

0
 
NerdsOfTechTechnology ScientistCommented:
Now we are really to format this data!

:)
newlines-removed-after-multiwhit.png
0
 
NerdsOfTechTechnology ScientistCommented:
The numbers 1234567..blah are confusing is there a way to make this example more unquie?

I want to analyze the patterns to match these rows correctly.

Basically, I want to know which numbers from section I match the subsequent sections II, and III.

Thanks
0
 
weklicaAuthor Commented:
Yes, I will come up with some better examples.
0
 
weklicaAuthor Commented:
We didn't really get too far with this, but thanks for your efforts.  I will have a couple of more side projects for you to do in the near future if you are up to it.  I will get you the details when I am ready so you can quote it out.  Thanks
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now