Jump to content

Recommended Posts

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 by pitekz
Link to comment
https://forums.phpfreaks.com/topic/294386-find-and-replace-in-a-file/
Share on other sites

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.