Re: [PHP] gobbling replace

2002-05-10 Thread Josh Valerie McCormack

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 456br
 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 [EMAIL PROTECTED]
 Date:

 Thu, 9 May 2002 23:39:34 -0400
 To:

 PHP List [EMAIL PROTECTED]


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 Im 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


 






Re: [PHP] gobbling replace

2002-05-10 Thread Analysis Solutions

Hey Josh:

On Fri, May 10, 2002 at 08:19:59AM -0400, Josh  Valerie McCormack wrote:
 Thanks Dan. It's really close now. But it's duplicating the text string 
 at the beginning and end of the replaces.
 ... snip ...
10 123 123 Shawn Adams 456 456 br

No, you mean to say that you're replacing the strings twice.  The question is why?

Two possible reasons.  The csv file has the names in there twice or the lines in it 
are longer than 1000, so the fgetcsv() hits the line twice before getting to the end.  
Or, perhaps some other strange thing about the csv file.  Oh, of course, the extra 
123's could be in the original file.

Before I forget, if you ever post code or files again, don't put in the line 
numbers.  They get in the way of reading it and if for some reason I wanted to run 
your code on my system, I'd have to get rid of them first.


  7 $num = count ($data);

Why do you have this line?  It's unnecessary.


 by the way, were you saying I don't need the fcloses?

If your script is about to end, no, you don't need to use fclose().

And two more pointers, don't top post messages and don't endlessly quote the prior 
message.  Delete irrelevant parts and place your comments about particular relevant 
parts just below each relevancy.

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




[PHP] gobbling replace

2002-05-09 Thread Josh Valerie McCormack

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 Im 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




Re: [PHP] gobbling replace

2002-05-09 Thread Analysis Solutions

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 Im 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




Re: [PHP] gobbling replace

2002-05-09 Thread Analysis Solutions

On Thu, May 09, 2002 at 11:39:34PM -0400, Analysis  Solutions wrote:
 On Thu, May 09, 2002 at 03:17:09PM -0400, Josh  Valerie McCormack wrote:

   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

Pardon me, I meant to say field rather than record.

--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