Re: Question about string replace -PHP

From: Date: Tue, 14 May 2002 21:20:43 +0000
Subject: Re: Question about string replace -PHP
Groups: php.general 
Request: Send a blank email to php-general+get-97628@lists.php.net to get a copy of this message
I didn't ask the question about this, but I have a related issue. I'm trying to do a replace, and am using str_replace, but the outcome is not what I expected. Here's my code: <?
$fp     = fopen("new_data.csv", "r");
$text = fopen("text.txt", "r"); $merged = fopen("merged_text.html", "w"); $filecontents = fread($text,filesize("text.txt")); while ($data = fgetcsv ($fp, 1000, ",")) {
       $num = count ($data);
       $fullname = $data[0] . ' ' . $data[1];
       $newfullname = '123 ' .  $fullname . ' 456';
       $filecontents = str_replace($fullname, $newfullname, $filecontents);
} fclose ($fp); fwrite($merged,$filecontents); fclose ($merged); fclose ($text); ?> Here's the text file it's working on: John Smith <br> <br> Peter Cooper <br> <br> Mike Brown <br> <br> Lisa Crow <br> Here's the outcome: 123 123 John Smith 456 456 <br> <br> 123 123 Peter Cooper 456 456 <br> <br> 123 123 Mike Brown 456 456 <br> <br> 123 123 Lisa Crow 456 456 <br> I only want it to have one set of 123 and 456 on either side. Any ideas? Josh

« previous php.general (#97628) next »