replace strings in looped fgetcsv results

From: Date: Fri, 03 May 2002 04:46:03 +0000
Subject: replace strings in looped fgetcsv results
Groups: php.general 
Request: Send a blank email to php-general+get-95823@lists.php.net to get a copy of this message
I have a text file with info and people's names in it. I have a csv file with people's names in it. I want to open both, read the text file into a variable, iterate through the csv file with fgetcsv and replace the names in the text file with data from the csv file. so, I have a csv file with fields first name, last name, url. I have the text file with first name last name. I want the names linked and it saved to a new file. I've tried moving the writes around, the replaces, using different types of replaces, etc. It's just not quite working out. Here's what I have so far:
     1 <?
     2 $row = 1;
     3
     4 $text   = fopen("text.txt", "r");
     5 $merged = fopen("merged_text.html", "w");
     6 $fp     = fopen("new_data.csv", "r");
     7
     8 $filecontents = fread($text,filesize("text.txt"));
     9
    10 while ($data = fgetcsv ($fp, 1000, ",")) {
    11         $num = count ($data);
    12         $row++;
    13         for ($c=0; $c < $num; $c++) {
    14                 $fullname = $data[0] . " " . $data[1];
    15                 $newfullname = "<a href=".$data[2].">".$fullname."</a>";
    16                 $newfilecontents = str_replace($fullname,$newfullname,$filecontents);
    17
    18                 fwrite($merged,$newfilecontents);
    19         }
    20 }
    21 echo nl2br($newfilecontents);
    22 // fwrite($merged,$newfilecontents);
    23 fclose ($fp);
    24 fclose ($merged);
    25 fclose ($text);
    26 ?>
Thanks! Josh

« previous php.general (#95823) next »