On Thu, May 09, 2002 at 03:17:09PM -0400, Josh & Valerie McCormack wrote:
> I'm doing a str_replace (same thing happens with ereg and preg) and when 
> the pattern is found it's erasing everything after it and doing a 
> strange replace, too.
> 
> I'm using a seven column CSV (new_data.csv) where the first [0] column 
> is first name and the second [1] is last name. This is what I'm trying 
> to search for, and replace with with some characters on either side. So 
> it will find John Smith and replace it with 123 John Smith 456.
> 
> Here's the file I"m trying to do the replaces in (text.txt):
>      1 This is a test of the templating program. <br>
>      2 Here's hoping it's working. <br>
>      3 <br>
>      4 <br>
>      5
>      6 John Smith <br>
>      7
>      8 Peter Lem <br>
>      9
>     10 Shawn Adams <br>
> 
> Here's the program:
>      1 <?
>      2 $row = 1;
>      3 $fp     = fopen("new_data.csv", "r");
>      4 while ($data = fgetcsv ($fp, 1000, ",")) {
>      5         $num = count ($data);
>      6         $row++;
>      7         for ($c=0; $c < $num; $c++) {
>      8                 $text   = fopen("text.txt", "r");
>      9                 $merged = fopen("merged_text.html", "w");
>     10                 $filecontents = fread($text,filesize("text.txt"));
>     11                 $fullname = $data[0] . ' ' . $data[1];
>     12                 $newfullname = "123 " .  $fullname . " 456";
>     13                 $new_filecontents = str_replace($fullname, 
> $newfullname, $filecontents);
>     14                 fwrite($merged,$new_filecontents);
>     15                 fclose ($merged);
>     16                 fclose ($text);
>     17                 copy("merged_text.html", "text.txt");
>     18         }
>     19 }
>     20 fclose ($fp);
>     21 ?>
> 
> by the way, I know it's horribly ugly to open and close the file in the 
> loop like that, but when it was outside the loop no changes were saved.

Put ALL of the fopen()'s AND fread()'s before the while loop.

Put ALL of the fwrite()'s AND (unnecessary) fclose()'s after the end of the while 
loop.

The only thing you should be doing inside the loop is searching/replacing.  The while 
aspect will automaticall bring up the next names to be searching for.

That for loop shouldn't be there...


> Here's the ouput (merged_text.html):
>      1 This is a test of the templating program. <br>
>      2 Here's hoping it's working. <br>
>      3 <br>
>      4 <br>
>      5
>      6 123 123 123 123 123 123 John Smith 456 456 456 456 4

The odd replacement is due to the itterations for each record in the csv file caused
by mistakenly using that for loop.  Ditch it.

Enjoy,

--Dan

-- 
               PHP classes that make web design easier
        SQL Solution  |   Layout Solution   |  Form Solution
    sqlsolution.info  | layoutsolution.info |  formsolution.info
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
 4015 7 Av #4AJ, Brooklyn NY     v: 718-854-0335     f: 718-854-0409

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to