Jump to content

finding a line in a file


php_novice2007

Recommended Posts

Hi,

 

How would I write some php code that opens up a text file, looks for a specific line, and delete it?

 

Basically I have a text file called photoList.txt, which has a list of all the photos stored, one line at a time. And if I want to delete the photo, I basially have to delete it from photoList.txt.

 

I can't figure out the logic.. :(

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/43068-finding-a-line-in-a-file/
Share on other sites

here's one way

 

<?php
/**
* Suppose we want to delete c.jpg 
*/
$delete = 'c.jpg';

/**
* read file into an array
*/
$photos = file('photolist.txt');

/**
* find position of file to be deleted
*/
$index = array_search($delete."\n", $photos);

/**
* remove it from the array
*/
unset($photos[$index]);

/**
* write array to the file
*/
file_put_contents('photolist.txt', $photos);

?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.