Skip to content

PHP function to delete a line containing a specific string in any kind of file

Notifications You must be signed in to change notification settings

yjajkiew/php-delete-line-in-file

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 

Repository files navigation

PHP Function: delete a specific line in a file

How to use it

Just put the function's code from the "php-delete-line-in-file.php" file in yours and call the function like this:

deleteLineInFile("myFile.ext","myString");

How it works

  1. read the file line per line
  2. store each line in an array

This way the file is backed up so it can be opened for writing, erasing all the data

  1. for each line stored in the array, if it doesn't contain the string we write it back in the file
  2. else we do nothing so only the lines not containing the string are written back

Why I did it

I had a CSV file who looked like this:

text1;text2

text3;text4

text5;text6

And I needed to remove the line containing the "text3" string. I did my function so it can work with my specific file, and then I changed it to work with any kind of file (tested with CSV, TXT and HTML files).

License

       DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
               Version 1, August 2012

          Yann Jajkiewicz <contact@yann.me>

Everyone is permitted to copy and distribute verbatim or modified
copies of this program.

        DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
 TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.

About

PHP function to delete a line containing a specific string in any kind of file

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages