Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse text file - Am I looking in the right spot?

Status
Not open for further replies.

axsom1

Technical User
Feb 27, 2001
67
US
Hi, I have been given the task of taking a text file that is generated daily, and removing some garbage text out of it.

I really don't know where to begin. I originally thought PHP would be my best source, but don't see how I can remove blocks of text, say from line 1-70.

Any ideas?

Thanks,
John
 
With most programming languages, you can't remove text from a file. What you do is stream the old file to a new one, making modifications as the data passes through your script. Then you delete the old file and rename the new file to the old file's filename.


Want the best answers? Ask the best questions: TANSTAAFL!!
 
uhm.. to remove 1-70, do smthing like:

Code:
<?
  $f = fopen(&quot;path/to/file&quot;,&quot;r+&quot;);
   $text = fread($f, filesize(&quot;path/to/file&quot;));
// now you have the file contents in $text, now:
   $text = explode(&quot;\n&quot;, $text);
// $text will become an array

for ($i = 0; $i < count($text); $i++) {
  if ($i >= 69) { 
   
    echo $text[$i]; // or do whatever you want

// if you need to re.insert the lines into the file, just:
   $newtext .= $text[$i].&quot;\n&quot;;

  }
}   
   
  fputs($f, $newtext);
  fclose($f);
?>

think that'll do ;)

jamesp0tter,
jamespotter@netcabo.pt

p.s.: sorry for my (sometimes) bad english :p
 
Ohhhh, ok, makes sense actually.

So, what would you recommend for me to get started? Like a for...loop or if...then statement?

I'm a newb to PHP so bear with the simple questions.

Thanks for your help,
John
 
Whoa, sorry James, I did not see your post when I replied to the one above.

So from what I see, I can specify a pointer starting point and ending point based on some text pattern.

Now, with the above statements, if I don't know the exact line position, but the cursor position say at 55 bytes of the file then ends at 80 bytes. How would I get rid of that?

Thanks for all the help though!
John

 
Well the following code obviously is wrong...maybe someone can help me turn on the light?

<?

$f = fopen(&quot;bad_text.txt&quot;, &quot;r&quot;);
$bad_text = fread($f, filesize(&quot;bad_text.txt&quot;));

$bad_text = explode(&quot;\n&quot;, $bad_text);

fclose($f);

$f = fopen(&quot;ub92.txt&quot;, &quot;r+&quot;);

while (!feof($f))
{

$ub92_text = fgets($f, filesize(&quot;ub92.txt&quot;));

for ( $i = 0; $i<count($bad_text); $i++ )
if ($bad_text[$i]<>$ub92_text)
echo $ub92_text.&quot;<br>&quot;;
}

fclose($f);


?>

Now, when I comment out the last for and if statement I see what I expect, but how come when I try to compare the array to the current line I get unexpected results?

I counted the block of text that is bad and it is currently 70 lines, but this 70 lines is spread throughout a 300k line text file. So I thought I could take and plop the bad text in an array and then compare that to the text file as it reads each line...am I way off base in my thinking?

I was hoping I could do what I need to accomplish this way...or a better way which I'm sure there is.

Thanks,
John
 
Can someone help me understand what I need to do to keep from getting the following error:

Notice: Undefined offset: X in D:\Web Sites\babycam_admin\ub92.php on line 19

where X is equal to the array element id differences. Below is the code I am using:

<?

$f = fopen(&quot;bad_text.txt&quot;, &quot;r&quot;);
$bad_text = fread($f, filesize(&quot;bad_text.txt&quot;));

$bad_text = explode(&quot;\n&quot;, $bad_text);

fclose($f);

$f = fopen(&quot;ub92.txt&quot;, &quot;r+&quot;);

$ub92_text = fread($f, filesize(&quot;ub92.txt&quot;));

$ub92_text = explode(&quot;\n&quot;, $ub92_text);

$result = array_diff($ub92_text, $bad_text);

for ( $i = 0; $i<count($result); $i++ )
echo &quot;$result[$i] &quot;.&quot;<br>&quot;;

fclose($f);

?>

If I can keep from getting the udeffined offset errors I will have exactly what I need.

Thanks,
John
 
Allright, well here is my final code. It works as I would like it to for right now. Maybe someone can just look over it and suggest some improvements?

<?

$f = fopen(&quot;bad_text.txt&quot;, &quot;r&quot;);
$bad_text = fread($f, filesize(&quot;bad_text.txt&quot;));

$bad_text = explode(&quot;\n&quot;, $bad_text);

fclose($f);

$f = fopen(&quot;ub92.txt&quot;, &quot;r+&quot;);

$ub92_text = fread($f, filesize(&quot;ub92.txt&quot;));

$ub92_text = explode(&quot;\n&quot;, $ub92_text);

$result = array_diff($ub92_text, $bad_text);

fclose($f);

//for ( $i = 0; $i<count($result); $i++ )
// echo &quot;$result[$i] &quot;.&quot;<br>&quot;;

$f = fopen(&quot;ub92.txt&quot;, &quot;w&quot;);

while ($output = each($result))
{
//echo $output[&quot;value&quot;].&quot;<br>&quot;;
fwrite($f, $output[&quot;value&quot;].&quot;\n&quot;);
}

fclose($f)

?>

Thanks to everyone for their help!!

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top