The Wayback Machine - https://web.archive.org/web/20240711081841/https://www.geeksforgeeks.org/how-to-delete-text-from-file-using-preg_replace-function-in-php/
Open In App

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

Last Updated : 14 Feb, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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


Previous Article
Next Article

Similar Reads

PHP | preg_replace() Function
The preg_replace() function is an inbuilt function in PHP that is used to perform a regular expression for search and replace the content. Syntax: preg_replace( $pattern, $replacement, $subject, $limit, $count ) Parameters: This function accepts five parameters as mentioned above and described below. $pattern: This parameter contains the string ele
2 min read
How to delete file from the firebase using file url in node.js ?
To delete a file from the Firebase storage we need a reference to store the file in storage. As we only have the file URL we need to create a reference object of the file in Firebase storage and then delete that file. Deleting a file using the file URL can be done in two steps - Get the reference to the storage using the refFromUrl method from Fire
2 min read
How to include content of a PHP file into another PHP file ?
Including the content of a PHP file into another file reduces the complexity of code by reducing the code and improving its modularity by splitting the code into different files so that it is easy to understand and manage. There are two ways to do it by using the following PHP functions. PHP include() Function: It will show the warning if the file
2 min read
How to replace a text in a string with another text using PHP ?
A string is a sequence of one or more characters. A string is composed of characters, each of which can be replaced easily in the script. A large number of in-built PHP methods can be used to perform string replacements. Table of Content Using str_replace() method - The str_replace() Using str_ireplace() method Using preg_replace()Using substr_repl
4 min read
How to generate PDF file from external text files using PHP ?
In this article, we will learn how to generate PDF files from external text files with PHP by using FPDF. It is a free PHP class that contains many functions for creating and modifying PDFs. The FPDF class includes many features like page formats, page headers, footers, automatic page break, line break, image support, colors, links, and many more.
5 min read
How to create a table in PDF file from external text files using PHP ?
In this article, we will learn to create a table in a PDF file from an external text file with PHP by using FPDF. It is a free PHP class that contains many functions for creating and modifying PDFs. The FPDF class includes many features like page formats, page headers, footers, automatic page break, line break, image support, colors, links, and man
3 min read
Which one method use to delete an element from JavaScript array (slice or delete) ?
We can use both methods to delete an element from a javascript array. The answer is solely dependent on the application and on the programmer. What slice() method does? The slice method, as the name suggests, slices an array and returns a copy of the new array. This method does not affect the main array, it just returns the copy of the array. We ca
4 min read
Delete the array elements in JavaScript | delete vs splice
This article will show you how to Delete the array element at a specific position using JavaScript. There are two approaches that can be used to delete elements in an array. They have their own merits regarding the way they perform the deletion. Delete array[index]:This method deletes the element at the index specified but does not modify the array
3 min read
PHP | Program to delete an element from array using unset() function
Given an array of elements, we have to delete an element from the array by using the unset() function. Examples: Input : $arr = array("Harsh", "Nishant", "Bikash", "Barun"); unset($arr[3]); Output : Array ( [0] =&gt; Harsh [1] =&gt; Nishant [2] =&gt; Bikash ) Input : $arr = array(1, 2, 6, 7, 8, 9); unset($arr[3]); Output : Array ( [0] =&gt; 1 [1] =
2 min read
How to delete text from document using HTML ?
In this article, we delete text from an HTML document by using the &lt;del&gt; tag in the document. This tag stands for delete and is used to mark a portion of text which has been deleted from the document. Syntax: &lt;del&gt; Contents... &lt;/del&gt; Example: In the following example, deletion of a text from a document is demonstrated. &lt;!DOCTYP
1 min read
three90RightbarBannerImg