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 */
?>