Jump to content


PHP Text handling...

  • Please log in to reply
1 reply to this topic

#1 vidyashankara

  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 20 June 2006 - 07:59 PM

I have a number of text files that are got from the previous page. I need a script to read this files, and make some operations. i have the following script

$files = $$HTTP_POST_VARS["files"]; //gets the file names.

foreach($files as $file) {
$contents=file_get_contents($file); //read contents into an string
$atomarray = explode("\n", $atomstart);  // read string into array

for ($i=0; $i<count($atomarray); $i++) { 
$resi[$i] = substr($atomarray[$i], 13, 3); // read the 14,15,16 character of each line

$resi = array_unique($resi); //get only unique results
sort($resi);  // sort results

$resi1 = implode(" ", $resi);  //seperate results with a space. 

if (strpos($resi1, " ABC ")) { 
    $str="Hi" // if ABC exists, $str = "hi"
if (strpos($resi1, "XYZ")) { 
        $str="Bye" // if XYA exists, $str = "bye"

$fp = f open("test.txt", "w");
f write ($fp, $str);
f close($fp);

The problem with this script is that when ask it to echo $resi1, It echos only the results from the last file. The script might have to read anywhere between 2 to 5 files, can you find anything wrong with the script?


#2 Kris

  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 June 2006 - 09:42 PM

Replace the string mode in fopen() with "a" instead of "w", "w" places the pointer at the begining of the file and truncates the file to 0 length, emptying it. "a" just places the pointer at the end of the file.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users