Solved

How to get a part of text from a file in PHP

Posted on 2012-03-12
19
177 Views
Last Modified: 2012-03-14
I am stumped.  I am trying to use File_get_content and Fgets to get a part of text from a file.

Here is the file:

BPR*I*331.53*C*ACH*CCP*01*124384877*DA*1470858534*1411289245*0000 87726*01*052000113*DA*18790741*20120105~TRN*1*1058202611*.

I am still very much learning and would really like to know how to get the bolded section from this text file.
I tried  $sec = file_get_contents('./Inputfile.txt', NULL, NULL, '~trn*', 150); but that does not work.  I also tried fgets but did not understand how I could get that to show in what I wanted.  

The end results would be:

ABC.txt is the filename
ABC_~TRN*1*1058202611*.txt would be the new filename.


 Please help.  Thanks
0
Comment
Question by:Sqlspider
  • 8
  • 6
  • 3
  • +1
19 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
If the part you want is always last and in that format, you can do this:

$sec = file_get_contents('./Inputfile.txt');
$sec2 = explode("~", $sec);
echo $sec2[1];

Open in new window

0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
Try using explode() to divide a string by a delimiter. In this case:

<?php
$data = file_get_contents("ABC.txt");
$pieces = explode("~",$data);
$last_piece = array_pop($pieces);

// $last_piece = str_replace("*","-",$last_piece);

rename("ABC.txt","ABC_~" . $last_piece . ".txt");
?>

Now, please note that this will not work immediately, because you have the * character in your filename, which is not a safe/valid character for filenames. However, if you uncomment the one line that I added, it will change the * to - so that your filename will be:

ABC_~TRN-1-1058202611-.txt
0
 

Author Comment

by:Sqlspider
Comment Utility
thanks for you quick response DaveBaldwin.  the txt I showed would have other txt after the *.  But in each file it will always have this part: ~TRN*1*1058202611*

Sorry I was not concise on that.
0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
If there is text after that bit, then instead of using:

$last_piece = array_pop($pieces);

use :

$last_piece = $pieces[1];
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
You have to be able define either something unique or a predictable pattern to be able to 'programmatically' extra data.  If the number of characters is the same every time, that would be enough to do a substr() on.
0
 

Author Comment

by:Sqlspider
Comment Utility
I understand both comments.  The ~ is in there several times so when I tried the above it gave me errors. Darn. I checked to verify a few other files and the number of characters are not the same of course.  I am really stumped here.
0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
Are you always looking for TRN*1* and then a long number? Or maybe TRN*Number*Number?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
@Sqlspider, please post enough of the test data so that we can recognize the patterns.  Please use the code snippet to post at least a few lines, thanks. ~Ray
0
 

Author Comment

by:Sqlspider
Comment Utility
Sorry Ray I have to edit most of the information due to the content but here is something that is very close to what I am working with.  My apologies to the rest of the group as well.

ISA*00*          *00*          *ZZ*1332274      *ZZ*5219248      *1111*222
0*U*00401*000036059*0*P*>~GS*HP*133274*521928*20111*222*1*X*004X091
A1~ST*835*0001~BPR*I*21.55*C*ACH*CCP*01*12437*DA*18534*141145*008
7726*01*052003*DA*18790*20115~TRN*1*1QG084*141245*000026~DT
M*405*20109~N1*PR*UNI COMPANY*XV*87726~N3*9900 BRE
OAD~N4*MINNA*MN*55300~REF*2U*87726~PER*CX*ATLR*TE*877
8420~N1*PE*SICIA*XX*1558362111~N3* BLVD
 STE 200~N4*MILLE*MD*211000~REF*TJ*521923448~LX*1~TS3*152111*99*20
1131*1*145~CLP*M28872*2*145*21.55**14*3011766 0019585*99~NM1*QC*1*HE*R
O****MI*956133~NM1*82*1*DIER*STEN*H***XX*1558311~REF*1L*715567~REF
*G1*H48015~REF*CE*CHOYC~DTM*050*20111~DTM*232*20106~AMT*AU*107.76~SVC*HC>9
9214*145*21.55**1~DTM*472*201116~CAS*OA*23*123.45~REF*6R*001~AMT*B6*107.76~SE*
30*0001~ST*835*0002~BPR*H*0*C*NON************20115~TRN*1*1Q79736*12892
45*000026~DTM*405*20109~N1*PR*UNI COMPANY*XV*87726
~N3*9900 BRAD~N4*MINKA*MN*5530~REF*2U*87726~PER*CX*ATLR*TE*8778423210~N1*PE**XX*1871512269~N3*111
1 BSTE 200~N4*MILL*MD*2110~REF*TJ*528~LX*1~TS3*1
8769*11*20231*3*260~CLP*A024253*4*100*0**12*301855 01204*11~NM1
*QC*1*E****MI*82300~NM1*IL*1*SLL JR*MRS ROBERT****MI*82
3350300~NM1*74*1******C*814041783~NM1*82*1*E*AMADA****XX*1871269~REF*1L
*046000~REF*G1*H88686~REF*CE*OPT PPO~DTM*050*2011~DTM*232*201013~SVC*HC>99
213*100*0**1~DTM*472*201~CAS*CO*18*100~REF*6R*001~LQ*HE*M86~CLP*A048752*2*1
5*0**HM*306351 0014034*99~NM1*QC*1*HAFF*ANTTE****MI*846899~NM
1*IL*1*HAF JEORGE****MI*846099~NM1*82*1*MANE*AMA****XX*187226
9~REF*1L*712403~REF*G1*E68813~REF*CE*MDIPA~DTM*050*2011~DTM*232*20110927~AMT
*AU*3~SVC*HC>36415*15*0**1~DTM*472*2027~CAS*OA*23*15~REF*6R*001~AMT*B6*3~CLP
*A04852*2*145*0**HM*3011351 001042*99~NM1*QC*1*HAFF*ANTTE****MI
*846067899~NM1*IL*1*HAEORGE****MI*84609~NM1*82*1*ME*AMADA***
*XX*1871512269~REF*CE*MDIPA~REF*G1*E68813~REF*1L*712403~DTM*050*201~DTM*232
*207~AMT*AU*107.76~SVC*HC>99214>25*145*0**1~DTM*472*20110927~CAS*CO*24*21.5
5~CAS*OA*23*123.45~REF*6R*004~AMT*B6*107.76~SE*62*0002~ST*835*0003~BPR*I*30*C*AC
H*CCP*01*124384877*DA*147534*141145*0006*01*0523*DA*181*2
05~TRN*1*1Q804*14145*00726~DTM*405*2009~N1*PR*UNIY*XV*87726~N3*9900 BD~N4*MINNEA*MN*550~
REF*2U*87726~PER*CX*ATLER*TE*8778423210~N1*PE*M*XX*1215929112~N3*1111  200~N4*MILLE*MD*211
080000~REF*TJ*521923448~LX*1~TS3*129112*11*201231*2*200~CLP*M176975*2*100*
15*5*14*3011097580 0042375677*11~NM1*QC*1*KADER*RANL****MI*000043678~NM1
*IL*1*KALN****MI*000043678~NM1*82*1*POBE*PIO*L***XX*12112~REF
*G1*G25721~REF*CE*SEL EPO~REF*1L*704567~DTM*050*2013~DTM*232*20110902~AMT*AU
*20~SVC*HC>99213*100*15**1~DTM*472*20110902~CAS*OA*23*80~CAS*PR*3*5~REF*6R*0LTREIDER*
R103~DTM*232*20110725~AMT*AU*20~SVC*HC>99213*100*15**1~DTM*472*2025~CAS*OA*23
*80~CAS*PR*3*5~REF*6R*001~AMT*B6*20~SE*48*0003~GE*48*1~IEA*1*00059~
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Just a thought for going forward... Consider setting up a test data set. so there is no risk of editing errors in the process of redacting the the test data.
0
 

Author Comment

by:Sqlspider
Comment Utility
Very Good Ray_Paseur, I continue to get good instruction from you.  Thank you.  For some reason i can not get the code snippet to work properly on my machine.
I tried to give a better example below of what I am trying to do.  Hopefully this gives a better showing...


File 1) ~TRN*1*1QG084   the filename would be (ABC_~TRN*1*1QG084.txt)

~ST*835*0001~BPR*I*21.55*C*ACH*CCP*01*12437*DA*18534*141145*008
7726*01*052003*DA*18790*20115~TRN*1*1QG084*141245*000026~DT
M*405*20109~N1*PR*UNI COMPANY*XV*87726~N3*9900 BRE
OAD~N4*MINNA*MN*55300~REF*2U*87726~PER*CX*ATLR*TE*877
8420~N1*PE*SICIA*XX*1558362111~N3*BLVDSTE200~N4*MILLE*MD*211000~REF*TJ*521923448~LX*1~TS3*152111*99*201131*1*145~CLP*M28872*2*145*21.55**14*3011766 0019585*99~NM1*QC*1*HE*RO****MI*956133~NM1*82*1*DIER*STEN*H***XX*1558311~REF*1L*715567~REF*G1*H48015~REF*CE*CHOYC~DTM*050*20111~DTM*232*20106~AMT*AU*107.76~SVC*HC>99214*145*21.55**1~DTM*472*201116~CAS*OA*23*123.45~REF*6R*001~AMT*B6*107.76~SE*30*0001

File 2) ~TRN*1*1Q79736    the filename would be (ABC_~TRN*1*1Q79736.txt)

~ST*835*0002~BPR*H*0*C*NON************20115~TRN*1*1Q79736*12892
45*000026~DTM*405*20109~N1*PR*UNI COMPANY*XV*87726
~N3*9900BRAD~N4*MINKA*MN*5530~REF*2U*87726~PER*CX*ATLR*TE*8778423210~N1*PE**XX*1871512269~N3*1111 BSTE 200~N4*MILL*MD*2110~REF*TJ*528~LX*1~TS3*1
8769*11*20231*3*260~CLP*A024253*4*100*0**12*301855 01204*11~NM1
*QC*1*E****MI*82300~NM1*IL*1*SLL JR*MRS ROBERT****MI*82
3350300~NM1*74*1******C*814041783~NM1*82*1*E*AMADA****XX*1871269~REF*1L
*046000~REF*G1*H88686~REF*CE*OPTPPO~DTM*050*2011~DTM*232*201013~SVC*HC>99213*100*0**1~DTM*472*201~CAS*CO*18*100~REF*6R*001~LQ*HE*M86~CLP*A048752*2*15*0**HM*306351 0014034*99~NM1*QC*1*HAFF*ANTTE****MI*846899~NM
1*IL*1*HAF JEORGE****MI*846099~NM1*82*1*MANE*AMA****XX*187226
9~REF*1L*712403~REF*G1*E68813~REF*CE*MDIPA~DTM*050*2011~DTM*232*20110927~AMT*AU*3~SVC*HC>36415*15*0**1~DTM*472*2027~CAS*OA*23*15~REF*6R*001~AMT*B6*3~CLP*A04852*2*145*0**HM*3011351 001042*99~NM1*QC*1*HAFF*ANTTE****MI
*846067899~NM1*IL*1*HAEORGE****MI*84609~NM1*82*1*ME*AMADA***
*XX*1871512269~REF*CE*MDIPA~REF*G1*E68813~REF*1L*712403~DTM*050*201~DTM*232*207~AMT*AU*107.76~SVC*HC>99214>25*145*0**1~DTM*472*20110927~CAS*CO*24*21.55~CAS*OA*23*123.45~REF*6R*004~AMT*B6*107.76~SE*62*0002
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
As mentioned before, '*' is not valid in filenames.  See here: http://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
0
 

Author Comment

by:Sqlspider
Comment Utility
DaveBaldwin, I am in agreement and I would incorporate what gr8gonzo gave
// $last_piece = str_replace("*","-",$last_piece);
to ensure the * is not in the final product.

I am going to try what you have given gr8gonzo to see if I will get the results I am looking for.
0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
Try this:

<?php
$oldfile = "ABC.txt";

if(preg_match("/(~TRN\*[^\*]+\*[^\*]+)/",file_get_contents($oldfile),$matches))
{
  // Match found! Renaming file!
  $newfile = str_replace(array(".txt","*"),array("_","-"),$oldfile.$matches[1]).".txt";
  echo "Renaming file from {$oldfile} to {$newfile}...";
}
else
{
  // No match found!
}
?>
0
 

Author Comment

by:Sqlspider
Comment Utility
That work without a hitch but question how do i get it to do that to each file in the directory.
One directory could have 25 files.  I see$oldfile = "ABC.txt" but I dont know how to call each file in that directory.  No manual human intervention rather when switching the file names.  

Again yes this work perfectly just dont know how to get it to run for the enitre directory.

Thanks gr8gonzo
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
Comment Utility
This would loop through all files ending in .txt

<?php

// Get an array of all files in the current directory that end in .txt
$txtfiles = glob("*.txt");

// Loop through the files
foreach($txtfiles as $oldfile)
{

// Skip files that already look like they've been processed
if(strpos($oldfile,"_~")) continue;

// Otherwise, process the rest of the files
if(preg_match("/(~TRN\*[^\*]+\*[^\*]+)/",file_get_contents($oldfile),$matches))
{
  // Match found! Renaming file!
  $newfile = str_replace(array(".txt","*"),array("_","-"),$oldfile.$matches[1]).".txt";
  echo "Renaming file from {$oldfile} to {$newfile}...";
}
else
{
  // No match found!
}
}

?>
0
 

Author Comment

by:Sqlspider
Comment Utility
Gr8gonzo that worked!  I still have lots to learn with PHP is see. The only problem is I can see the echo on screen but when I look at the filename in the directory it is still the same. I checked permissions and that directory is open for read, write and change. I thought the str_replace would have renamed the file the way I see it on the screen.  What did I miss here?
0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
No, str_replace() only sets up the variable containing the new file name. The script above is just a dry run, meaning that it does everything but actually rename the file, just so you can be sure that it works the way you want it to. Once you're ready to actually have it rename the file, you can use the rename() function:

echo "Renaming file from {$oldfile} to {$newfile}...";
rename($oldfile,$newfile);
0
 

Author Closing Comment

by:Sqlspider
Comment Utility
Thank you all for the help.  Learning everyday.  I am truly greatful!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 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

16 Experts available now in Live!

Get 1:1 Help Now