Replacing a string in a file with another string.

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
User avatar
Redcircle
Moderator
Moderator
Posts: 826
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA
Contact:

Wed Apr 02, 2003 6:22 pm

str_replace also works nice.

User avatar
quantm
New php-forum User
New php-forum User
Posts: 7
Joined: Mon Apr 07, 2003 8:00 pm
Location: Vietnam

Wed Apr 09, 2003 6:59 pm

I have wrote a function to replace an user password in passwd file under linux as follows:

function replace_passwd_full($filename,$username,$newpasswd)
{
$separator = ":";
$data=file($filename);
if (!($pipe = fopen($filename, "w"))) {
return false;
}
$size=count($data);
for($line=0;$line<$size;$line++) {
$dataline = $data[$line];
$dataline= preg_replace("/".$username.":(\S+):/i", $username.":".$newpasswd.":\$3", $dataline);
fputs($pipe,$dataline);
}
fclose($pipe);
return true;
}

Hope that helps!! :roll: :wink:
a diligent learner

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 2 guests