Jump to content

PHP Text handling...


vidyashankara

Recommended Posts

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

[code]
$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);
}
[/code]

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?

Thanks
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.