[PHP] Re: Question about string replace -PHP
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] gobbling replace
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
[PHP] gobbling replace
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] replace question
(This is more info on the question I asked before) Here's the text file (text.txt): This is a test of the templating program. Here's hoping it's working! John Smith Here's one line from the CSV file (new_data.csv): John,Smith,4770 Rosepetal Ct., Richmond, VA 22032 USA,(703) 978-4472,0/0/00, And here's the code I have (merge.php): 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=.$fullname..$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 ? Josh Miguel Cruz wrote: On Sun, 5 May 2002, Josh Valerie McCormack wrote: I'm iterating through a CSV file pulling in rows as arrays with fgetcsv and I'd like to search for patterns made up of the first two array items of each row with a space between them in a text file, and make them into links. I can't figure out how to do this, could someone help? How about posting a sample row or two along with how you want the data to appear after the transformation? miguel
[PHP] replace question
I'm iterating through a CSV file pulling in rows as arrays with fgetcsv and I'd like to search for patterns made up of the first two array items of each row with a space between them in a text file, and make them into links. I can't figure out how to do this, could someone help? Josh -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: file() and macintosh line break
I was having a tough time with a Mac file uploaded and squeezed into one line. My friend Shawn helped me write the following which cleans it up. Remove the echo statement to just change it and not see the change. Also, avoid trying to fix the file, make a new one, if you can. Hope this helps. Josh 1 ? 2 $data_file = data.csv; 3 $newdatafile = new_data.csv; 4 5 $file=fopen($data_file, r); 6 $newfile = fopen($newdatafile,w); 7 8 $filecontents = fread($file,filesize($data_file)); 9 echo $filecontents; 10 echo brbr; 11 $newfilecontents = preg_replace((\r\n|\n|\r),\n,$filecontents); 12 13 echo nl2br($newfilecontents); 14 15 fwrite($newfile,$newfilecontents); 16 fclose($file); 17 fclose($newfile); 18 ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] php/.htaccess/.htpasswd
I've used the script phtaccess, which I think used the mentioned class. Super easy to use. Josh On Wed, 1 May 2002, Kelly Meeks wrote: Is is possible to use php to admin a password file used by a .htaccess file? You should check the File_Passwd class from PEAR. http://chora.php.net/cvs.php/php4/pear/File -- Mika Tuupola http://www.appelsiini.net/~tuupola/
[PHP] replace strings in looped fgetcsv results
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php