Monday, February 23, 2026
HomeLanguagesHow to delete text from file using preg_replace() function in PHP ?

How to delete text from file using preg_replace() function in PHP ?

Given a file containing some elements and the task is to delete the content of the file using preg_replace() function. The preg_replace() function is searches the string pattern in the file and if string pattern found then it replace with the required string. In simple words it can modify the contents of a file.

Syntax:

preg_replace( $pattern,  $replacement, $subject);

Parameters:

  • $pattern: It contains the string we have to replace.
  • $replacement: It contains the string that will replace the existing string.
  • $subject: It contains the main string file where need to remove the string.

We have created a text file named as fruits.txt
fruits.txt

mango
apple
papaya
guava
apple
grapes
mango
apple

Program 1: This program removes all the strings from the given file.




<?php
  
$a = 'fruits.txt';
$b = file_get_contents('fruits.txt');
echo "File contents before using "
        . "preg_replace() function<br>";
echo $b;
echo "<br><br>File contents after using "
        . "preg_replace() function<br> ";
$c = preg_replace('/[a-z]/', '', $b);
echo $c;
file_put_contents($a, $c);
?>


Output:

File contents before using preg_replace() function
mango apple papaya guava apple grapes mango apple

File contents after using preg_replace() function

Program 2: This program deletes a specific content from the file using preg_replace() function.




<?php
  
$a = 'fruits.txt';
   
$b = file_get_contents('fruits.txt');
  
echo "File contents before using "
        + "preg_replace() function<br>";
echo $b;
  
echo "<br><br>File contents after using "
        + "preg_replace() function<br>";
  
$c = preg_replace('/[a]/', '', $b);
echo $c;
  
file_put_contents($a, $c); 
?>


Output:

File contents before using preg_replace() function
mango apple papaya guava apple grapes mango apple

File contents after using preg_replace() function
mngo pple ppy guv pple grpes mngo pple

Program 3: This program deletes the entire word from the file.




<?php
  
$a = 'fruits.txt';
   
$b = file_get_contents('fruits.txt');
  
echo "File contents before using "
        . "preg_replace() function<br>";
echo $b;
  
echo "<br><br>File contents after using "
        . "preg_replace() function<br>";
  
$c = preg_replace('/apple/', '', $b);
echo $c;
  
file_put_contents($a, $c); 
?>


Output:

File contents before using preg_replace() function
mango apple papaya guava apple grapes mango apple

File contents after using preg_replace() function
mango papaya guava grapes mango
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS