Re: Search & replace text
| From: | David Robley | Date: | Mon, 12 Feb 2001 00:22:23 +0000 |
| Subject: | Re: Search & replace text | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-39155@lists.php.net to get a copy of this message | ||
On Mon, 12 Feb 2001 06:41, CDitty wrote:
> 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);
You need to assign the output of this function to a string:
$new_line = str_replace($oldemail, $newemail, $line);
> }
>
> echo $line . "<BR>";
and then
echo $new_line . '<BR>;
> }
> fclose($file)
--
David Robley | WEBMASTER & Mail List Admin
RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/
AusEinet | http://auseinet.flinders.edu.au/
Flinders University, ADELAIDE, SOUTH AUSTRALIA