Jump to content


This topic is now archived and is closed to further replies.


PHP Text handling...

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

$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?


Share this post

Link to post
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.

Share this post

Link to post
Share on other sites


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.