Re: gobbling replace
| From: | Josh & Valerie McCormack | Date: | Fri, 10 May 2002 12:54:53 +0000 |
| Subject: | Re: gobbling replace | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-96942@lists.php.net to get a copy of this message | ||
Thanks Dan. It's really close now. But it's duplicating the text string at the beginning and end of the replaces.
Here's the ouput:
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 John Smith 456 456<br>
7
8 123 123 Peter Lem 456 456 <br>
9
10 123 123 Shawn Adams 456 456 <br>
And here's the rewritten code:
1 <?
2 $fp = fopen("new_data.csv", "r");
3 $text = fopen("text.txt", "r");
4 $merged = fopen("merged_text.html", "w");
5 $filecontents = fread($text,filesize("text.txt"));
6 while ($data = fgetcsv ($fp, 1000, ",")) {
7 $num = count ($data);
8 $fullname = $data[0] . ' ' . $data[1];
9 $newfullname = '123 ' . $fullname . ' 456';
10 $filecontents = str_replace($fullname, $newfullname, $filecontents);
11 }
12 fclose ($fp);
13 fwrite($merged,$filecontents);
14 fclose ($merged);
15 fclose ($text);
16 ?>
by the way, were you saying I don't need the fcloses?
Thanks!
Josh
Subject: Re: [PHP] gobbling replace From: Analysis & Solutions <danielc@analysisandsolutions.com> Date: Thu, 9 May 2002 23:39:34 -0400 To: PHP List <php-general@lists.php.net> On Thu, May 09, 2002 at 03:17:09PM -0400, Josh & Valerie McCormack wrote: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...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> 910 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.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 ------------------------------------------------------------------------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