pitekz Posted February 4, 2015 Share Posted February 4, 2015 (edited) Hi I have a problem, because I need a script that will find data stored in a file and replace it by entering another data which is name and surname. Basically I have done a php code but it doesnt seem to work, and if possible could any one help me write a html form to change the name and surname. HTML code: <!DOCTYPE HTML> <html> <head> <!-- title of the web page--> <title>input</title> </head> <body> <form name="form1" method="post" action="replace.php"> First Name: <input type="text" name="name"><br /> Surname: <input type="text" name="sname"><br /> <input type="submit" name="Submit" value="Submit" /> </form> </body> </html> PHP Code: <?php $name = $_POST['name']; $sname = $_POST['sname']; $file = "data.txt"; /* original file */ $file2 = "somefile2.txt"; /* secondary file you mentioned */ $haystack = file($file); /* put contents into an array */ $needle1 = "$name, $sname"; /* value to be replaced */ $needle2 ="replaced, replaced"; /* value doing the replacing can be replaced by new variables*/ for($i=0; $i<count($haystack); $i++) { $haystack[$i] = str_ireplace($needle1, $needle2, $haystack[$i]); /* case insensitive replacement */ } $content = implode($haystack); /* put array into one variable with newline*/ //$content = implode("\n", $haystack); /* put array into one variable with newline*/ file_put_contents($file, $content); /* over write original file with changes made */ file_put_contents($file2, $content); /* write to second file */ ?> Edited February 4, 2015 by pitekz Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 5, 2015 Share Posted February 5, 2015 Your code suggests you are trying to do this: Replace "john, smith" with some values such as 'replaced, replaced'. Is that really what you are expecting to find in your file? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted February 5, 2015 Share Posted February 5, 2015 First, clarify what "but it doesnt seem to work" means. Report any errors using: error_reporting(E_ALL); ini_set('display_errors', '1'); Also, no need to loop, you can use arrays in the str_ireplace: $haystack = file($file); /* put contents into an array */ $search = "$name, $sname"; /* value to be replaced */ $replace ="replaced, replaced"; /* value doing the replacing can be replaced by new variables*/ $haystack = str_ireplace($search, $replace, $haystack); /* case insensitive replacement */ But one problem is, what if $name and $sname are on different lines? No replacement is made. Maybe just use the file as a string: $haystack = file_get_contents($file); /* put contents into a string */ $search = "$name, $sname"; /* value to be replaced */ $replace ="replaced, replaced"; /* value doing the replacing can be replaced by new variables*/ $haystack = str_ireplace($search, $replace, $haystack); /* case insensitive replacement */ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.