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

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
SqlspiderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
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

gr8gonzoConsultantCommented:
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
SqlspiderAuthor Commented:
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.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

gr8gonzoConsultantCommented:
If there is text after that bit, then instead of using:

$last_piece = array_pop($pieces);

use :

$last_piece = $pieces[1];
Dave BaldwinFixer of ProblemsCommented:
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.
SqlspiderAuthor Commented:
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.
gr8gonzoConsultantCommented:
Are you always looking for TRN*1* and then a long number? Or maybe TRN*Number*Number?
Ray PaseurCommented:
@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
SqlspiderAuthor Commented:
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~
Ray PaseurCommented:
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.
SqlspiderAuthor Commented:
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
Dave BaldwinFixer of ProblemsCommented:
As mentioned before, '*' is not valid in filenames.  See here: http://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
SqlspiderAuthor Commented:
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.
gr8gonzoConsultantCommented:
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!
}
?>
SqlspiderAuthor Commented:
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
gr8gonzoConsultantCommented:
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!
}
}

?>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SqlspiderAuthor Commented:
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?
gr8gonzoConsultantCommented:
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);
SqlspiderAuthor Commented:
Thank you all for the help.  Learning everyday.  I am truly greatful!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.