I am finding certain text and replacing it in the file. But it gets written three times in the file. I am sending the code.
*******************HTML CODE********************************* - <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
-
<html>
-
<head>
-
<title>Untitled Document</title>
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
-
</head>
-
-
<body>
-
<form name="form1" method="post" action="create.php">
-
<p> </p>
-
<p>File User Name
-
-
<input type="text" name="fname">
-
Replace
-
<input type="text" name="rname">
-
-
</p>
-
<p> File Password
-
<input type="text" name="fpasswd">
-
Replace
-
<input type="text" name="rpasswd">
-
-
-
<input type="submit" name="Submit" value="Submit">
-
-
</p>
-
</form>
-
</body>
-
</html>
@@@@@@@@@@@HTML FINISHED@@@@@@@@@@@@@
****************************PHP CODE************************************** - <?php
-
//Retrieve fields from the form
-
$fname = $_POST['fname'];
-
$runame = $_POST['rname'];
-
$fpass = $_POST['fpasswd'];
-
$rpass = $_POST['rpasswd'];
-
-
// file
-
$fh = fopen("sip.CONF", "r") or exit("Unable to open file!");
-
$fh1 = fopen("c:/sip1.CONF", "w") or exit("Unable to open file!");
-
//while(!feof($fh))
-
// {
-
-
$pattern1 = "/".$fname."/";
-
$pattern2 = "/".$fpasswd."/";
-
-
$replacement1 = $rname;
-
$replacement2 = $rpasswd;
-
-
-
$line= fgets($fh);
-
$line1 = preg_replace($pattern1, $replacement1, $line);
-
$line2 = preg_replace($pattern2, $replacement2, $line);
-
-
-
-
fwrite($fh1, $line1);
-
fwrite($fh1, $line2);
-
fwrite($fh1, $line3);
-
// }
-
fclose($fh);
-
-
copy("c:/sip1.CONF","C:/wamp/www/Practice/sip.CONF");
-
-
?>
@@@@@@@@@@@@PHP CODE FINISHED@@@@@@@@@@@@@
7 2523 kovik 1,044
Recognized Expert Top Contributor
Which variable is being written three times?
And where do you set the $line3 variable?
pbmods 5,821
Recognized Expert Expert
Heya, rinkudhimar.
I award 15 points for creative use of '*****' and '@@@@@' to designate code blocks.
Unfortunately, I have to deduct 15 points for failure to use [code] tags.
Better luck next time.
Now please try this. This is the updated one*******************HTML CODE********************************* - <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
-
<html>
-
<head>
-
<title>Untitled Document</title>
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
-
</head>
-
-
<body>
-
<form name="form1" method="post" action="create.php">
-
<p> </p>
-
<p>File User Name
-
-
<input type="text" name="fname">
-
Replace
-
<input type="text" name="rname">
-
-
</p>
-
<p> File Password
-
<input type="text" name="fpasswd">
-
Replace
-
<input type="text" name="rpasswd">
-
-
-
<input type="submit" name="Submit" value="Submit">
-
-
</p>
-
</form>
-
</body>
-
</html>
@@@@@@@@@@@HTML FINISHED@@@@@@@@@@@@@
****************************PHP CODE************************************** - <?php
-
//Retrieve fields from the form
-
$fname = $_POST['fname'];
-
$runame = $_POST['rname'];
-
$fpass = $_POST['fpasswd'];
-
$rpass = $_POST['rpasswd'];
-
-
// file
-
$fh = fopen("sip.CONF", "r") or exit("Unable to open file!");
-
$fh1 = fopen("c:/sip1.CONF", "w") or exit("Unable to open file!");
-
while(!feof($fh))
-
{
-
-
$pattern1 = "/".$fname."/";
-
$pattern2 = "/".$fpasswd."/";
-
-
$replacement1 = $rname;
-
$replacement2 = $rpasswd;
-
-
-
$line= fgets($fh);
-
$line1 = preg_replace($pattern1, $replacement1, $line);
-
$line2 = preg_replace($pattern2, $replacement2, $line);
-
-
-
-
fwrite($fh1, $line1);
-
fwrite($fh1, $line2);
-
-
}
-
fclose($fh);
-
-
copy("c:/sip1.CONF","C:/wamp/www/Practice/sip.CONF");
-
-
?>
@@@@@@@@@@@@PHP CODE FINISHED@@@@@@@@@@@@@[/quote]
kovik 1,044
Recognized Expert Top Contributor
No.
Answer the questions I have asked you. Without that information, I don't know where to look.
$line1 and $line2 variables are being written 3 times and $line3 is of no use. I kept $line3 for IP. But now I don't want to use IP details .so discard IP($line3)
No.
Answer the questions I have asked you. Without that information, I don't know where to look.
kovik 1,044
Recognized Expert Top Contributor
Each time it goes through the while loop, it will add those lines to the file. Are you sure you want it to be in a while loop?
First of all Thanks for replying so quickly
It's necessary to put ehile loop as then and then only it will read till the end of the file. Otherwise not
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Eric Lilja |
last post by:
Hello, I had what I thought was normal text-file and I needed to locate a
string matching a certain pattern in that file and, if found, replace that
string. I thought this would be simple but I had...
|
by: Dorsa |
last post by:
HI, Could you please tell me the error in here. I am trying to open an
XML file from a link.
Response.Clear()
Response.Expires = 0
Response.BufferOutput = False
Response.ContentType =...
|
by: peter |
last post by:
Hello all,
I'm looking for an advice.
Example (one block in ascii file):
$------------------------
NAME='ALFA'
CODE='x'
$------------------------
|
by: TOXiC |
last post by:
Hi everyone,
First I say that I serched and tryed everything but I cannot figure
out how I can do it.
I want to open a a file (not necessary a txt) and find and replace a
string.
I can do it...
|
by: sheriff |
last post by:
Dear friends,
im a newbee for this forum and c++ im doing my MSc in Simulation Tech in mech. Engineering. My knowledge of c++ is very little which I had during my UG studies Long long ago .I am...
| |
by: Sanchit |
last post by:
I want to know thta how can i edit a file in C++
For Example my file is
Mr XyZ FFFFFF 65
And now i want go change this number 65 to 87.... how can i Do
this..... I...
|
by: Lee Crabtree |
last post by:
Is there a class in the framework that allows me read text from a file
in an unbuffered manner? That is, I'd like to be able to read lines in
the same manner as StreamReader.ReadLine(), but I also...
|
by: MikeJ |
last post by:
make a While loop
ofs = TextFileServer("somefile")
string srow
while (ofs=false)
{
srow=ofs.getRow();
Console.Writeline(srow);
}
|
by: mouac01 |
last post by:
Newbie here. How do I do a find and replace in a binary file? I need
to read in a binary file then replace a string "ABC" with another
string "XYZ" then write to a new file. Find string is the...
|
by: Ramesh |
last post by:
Hello,
I am using the ofstream class to create a text file with keys and
values like:
Key1=Value10
Key2=Value15
Key3=Value20
In case I need to set a new value for Key2, say value50 - I am...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |