473,473 Members | 1,604 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

File String Find and Replace

13 New Member
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*********************************
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <title>Untitled Document</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. </head>
  7.  
  8. <body>
  9. <form name="form1" method="post" action="create.php">
  10.   <p>&nbsp;</p>
  11.   <p>File User Name 
  12.  
  13.       <input type="text" name="fname">
  14.       Replace
  15.       <input type="text" name="rname">
  16.  
  17.   </p>
  18.   <p> File Password 
  19.       <input type="text" name="fpasswd">
  20.       Replace
  21.       <input type="text" name="rpasswd">
  22.  
  23.  
  24.       <input type="submit" name="Submit" value="Submit">
  25.  
  26.       </p>
  27. </form>
  28. </body>
  29. </html>
@@@@@@@@@@@HTML FINISHED@@@@@@@@@@@@@

****************************PHP CODE**************************************
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //Retrieve fields from the form
  3. $fname = $_POST['fname'];
  4. $runame = $_POST['rname'];
  5. $fpass = $_POST['fpasswd'];
  6. $rpass = $_POST['rpasswd'];
  7.  
  8. // file 
  9. $fh = fopen("sip.CONF", "r") or exit("Unable to open file!");
  10. $fh1 = fopen("c:/sip1.CONF", "w") or exit("Unable to open file!");
  11. //while(!feof($fh))
  12.  // {
  13.  
  14.       $pattern1 = "/".$fname."/";
  15.             $pattern2 = "/".$fpasswd."/";
  16.  
  17.     $replacement1 = $rname;
  18.         $replacement2 = $rpasswd;
  19.  
  20.  
  21.     $line= fgets($fh);
  22.     $line1 = preg_replace($pattern1, $replacement1, $line);
  23.     $line2 = preg_replace($pattern2, $replacement2, $line);
  24.  
  25.  
  26.  
  27.     fwrite($fh1, $line1);
  28.     fwrite($fh1, $line2);
  29.     fwrite($fh1, $line3);
  30.  // }
  31. fclose($fh); 
  32.  
  33. copy("c:/sip1.CONF","C:/wamp/www/Practice/sip.CONF");
  34.  
  35. ?>
@@@@@@@@@@@@PHP CODE FINISHED@@@@@@@@@@@@@
Jul 3 '07 #1
7 2523
kovik
1,044 Recognized Expert Top Contributor
Which variable is being written three times?

And where do you set the $line3 variable?
Jul 3 '07 #2
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.
Jul 4 '07 #3
rinkudhimar
13 New Member
Now please try this. This is the updated one*******************HTML CODE*********************************
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <title>Untitled Document</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. </head>
  7.  
  8. <body>
  9. <form name="form1" method="post" action="create.php">
  10.   <p>&nbsp;</p>
  11.   <p>File User Name 
  12.  
  13.       <input type="text" name="fname">
  14.       Replace
  15.       <input type="text" name="rname">
  16.  
  17.   </p>
  18.   <p> File Password 
  19.       <input type="text" name="fpasswd">
  20.       Replace
  21.       <input type="text" name="rpasswd">
  22.  
  23.  
  24.       <input type="submit" name="Submit" value="Submit">
  25.  
  26.       </p>
  27. </form>
  28. </body>
  29. </html>
@@@@@@@@@@@HTML FINISHED@@@@@@@@@@@@@

****************************PHP CODE**************************************
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //Retrieve fields from the form
  3. $fname = $_POST['fname'];
  4. $runame = $_POST['rname'];
  5. $fpass = $_POST['fpasswd'];
  6. $rpass = $_POST['rpasswd'];
  7.  
  8. // file 
  9. $fh = fopen("sip.CONF", "r") or exit("Unable to open file!");
  10. $fh1 = fopen("c:/sip1.CONF", "w") or exit("Unable to open file!");
  11. while(!feof($fh))
  12.  {
  13.  
  14.       $pattern1 = "/".$fname."/";
  15.             $pattern2 = "/".$fpasswd."/";
  16.  
  17.     $replacement1 = $rname;
  18.         $replacement2 = $rpasswd;
  19.  
  20.  
  21.     $line= fgets($fh);
  22.     $line1 = preg_replace($pattern1, $replacement1, $line);
  23.     $line2 = preg_replace($pattern2, $replacement2, $line);
  24.  
  25.  
  26.  
  27.     fwrite($fh1, $line1);
  28.     fwrite($fh1, $line2);
  29.  
  30.   }
  31. fclose($fh); 
  32.  
  33. copy("c:/sip1.CONF","C:/wamp/www/Practice/sip.CONF");
  34.  
  35. ?>
@@@@@@@@@@@@PHP CODE FINISHED@@@@@@@@@@@@@[/quote]
Jul 4 '07 #4
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.
Jul 4 '07 #5
rinkudhimar
13 New Member
$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.
Jul 5 '07 #6
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?
Jul 5 '07 #7
rinkudhimar
13 New Member
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
Jul 6 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

8
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...
11
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 =...
5
by: peter | last post by:
Hello all, I'm looking for an advice. Example (one block in ascii file): $------------------------ NAME='ALFA' CODE='x' $------------------------
3
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...
5
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...
4
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...
19
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...
4
by: MikeJ | last post by:
make a While loop ofs = TextFileServer("somefile") string srow while (ofs=false) { srow=ofs.getRow(); Console.Writeline(srow); }
3
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...
6
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...
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
1
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...
1
isladogs
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.