How to replace string in file using PHP

Last updated on August 30th, 2022 at 09:28 am

In this tutorial I will walk you through the process step by step to

Read file line by line

This is the first thing we have to do. Let us assume that the file that contains data is oldfile.txt . In order to read this file we are using file() function and assigning the data to $lines as an array. At the same time we are counting the number of lines using count()

$filename='oldfile.txt';
$lines = file($filename);
$l_count = count($lines);

Now to read this file it is simple just use a for loop

for($x = 0; $x<$l_count; $x++)
{
echo $lines[$x];
}

This will print every line from from oldfile.txt
So if I simply execute that script you should see a similar output. I am also displaying the content of oldfile.txt before executing the script to read it.

[email protected]:~# cat oldfile.txt
this is good
that is bad
nice program
python and php
jvm and java
this is great

[email protected]:~# php ./run.php
this is good
that is bad
nice program
python and php
jvm and java
this is great

[email protected]:~#

Replace string in the file

Now we have read the file our next agenda is to find the word that we are looking for within the file. For that we can use str_replace()

$string_to_find="and"
$replace_with="*mistonline.in-Test*";
$newstring = str_replace($string_to_find, $replace_with, $lines[$x],$count);

In the above snippet we are defining $string_to_find = “and“. This means that the word we are looking for is and within the file.

As you can see inside the file we have two lines which contains word and ,

python and php
jvm and java

So the complete code to read the file line by line, search for the word and then replace the word and with *mistonline.in-Test* is shown below

$filename='oldfile.txt';
$string_to_find="and";
$replace_with="*mistonline.in-Test*";
$lines = file($filename);
$l_count = count($lines);
for($x = 0; $x<$l_count; $x++)
{
$newstring = str_replace($line, $replace_with, $lines[$x],$count);
if ($count > 0) {
$stringData = "$newstring";
echo "<br>\n".$stringData;
}

Update these 2 variables as per your requirement with any words of your choice

$string_to_find="and"
$replace_with="*mistonline.in-Test*"

In this example when I execute the code I was able to extract lines that contains the word and , replace it with *mistonline.in-Test* then print it in STDOUT

[email protected]:~# php replace.php
python *mistonline.in-Test* php
jvm *mistonline.in-Test* java

[email protected]:~#

Save replaced lines to a new file

This is the final step, complete code with word replacement along with saving those extracted lines to a new file is shown below

<?php
$filename='oldfile.txt';
$myFile = "newfile.txt";
$string_to_find="and";
$replace_with="*mistonline.in-Test*";
$lines = file($filename);
$l_count = count($lines);
for($x = 0; $x<$l_count; $x++)
{
$newstring = str_replace($string_to_find, $replace_with, $lines[$x],$count);
if ($count > 0) {
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "$newstring";
echo "<br>\n".$stringData;
fwrite($fh, $stringData);
}
}
fclose($fh);
?>

As you might have noticed in the code we have defined $myFile = “newfile.txt” , this is the new file in which we save the replaced lines.

[email protected]:~# php ./run.php
python *mistonline.in-Test* php
jvm *mistonline.in-Test* java

[email protected]:~# cat newfile.txt
python *mistonline.in-Test* php
jvm *mistonline.in-Test* java

[email protected]:~#
Click to rate this tutorial!
[Total: 3 Average: 3.7]

Leave a Reply

Your email address will not be published. Required fields are marked *