RE: [PHP] Search & replace text

From: Date: Mon, 12 Feb 2001 09:41:55 +0000
Subject: RE: [PHP] Search & replace text
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-39188@lists.php.net to get a copy of this message
instead of writing to you the code I'll give you an idea: 1. you open the file and read it line-by-line assigning it to an array, 2. you loop that array parsing it line by line changing it's values with RegEx the way you want. 3. you open the same file and, looping the array writing it down overriding any existing data in the file. You are now set. Sincerely, Maxim Maletsky Founder, Chief Developer PHPBeginner.com (Where PHP Begins) maxim@phpbeginner.com www.phpbeginner.com -----Original Message----- From: CDitty [mailto:mail@redhotsweeps.com] Sent: Monday, February 12, 2001 5:12 AM To: php-general@lists.php.net Subject: [PHP] Search & replace text Hello all. I am trying to search through a text file on my server to replace the user email. I can open the file and read it, but cannot get it to "find" the actual string and replace it. Can someone look over my code and see what the problem is? I am afraid that ereg is not my strongest point. Also, at what point should I start writing the file out? Wouldn't there be a permissions error if I was reading and writing at the same time? Thanks CDitty $oldemail = "email1@mydomain.com"; $newemail = "email2@mydomain.com"; $user = "cditty"; $file = fopen("/path/to/the/user/file/$user.dat", "r"); if(!$file){ echo "<p>Unable to open remote file.\n"; exit; }else{ echo "Success<br>"; } while (!feof($file)) { $line = fgets($file, 255); if(eregi($oldemail, $line, $out)) { str_replace($oldemail, $newemail, $line); } echo $line . "<BR>"; } fclose($file) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#39188) next »