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.

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

Thanks for your help!

Josh


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

Reply via email to