Jump to content

Read & delete entries in txt file


KingdomTubes

Recommended Posts

Hello, I'm requesting your help here cause I'm really confused.

I'm looking for deleting entries when an image doesn't load on my links test.

 

I got all my links in a .txt file like this :

links.txt =

<div class="gallery">
<a href="http://www.website.com/page">
  <img src="http://tc16.easythumbhost.com/image.jpg" onerror="ImgError(this)"/>
</a>
</div>
#enddiv

 

the "onerror" code in img src sends an "Image Missing" pic.

What I'd want is to change the "onerror" function to open a php script which deletes all the lines of the div.

This is the script I use to show the links on my page :

 $links = "links.txt";
$fd = fopen ($links, "r");
$content = fread ($fd,filesize ($links));
fclose ($fd);
$delimiter = "#enddiv";
$splitcontent = explode($delimiter, $content);
$output = array_slice($splitcontent, 0,100);

foreach ( $output as $divs )
{
echo "$divs";
}

 

I've searched hours and tried lots of codes before posting

Help would be very appreciated. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/255902-read-delete-entries-in-txt-file/
Share on other sites

Since I had no idea how to make a database I continued this way, and this is the result.

I called this function "onerror"

function ImgError(data) {
  xmlhttp.open("POST","missing_image.php",false);
  xmlhttp.send('src='+data.src);
  data.src = "../../images/brokenlink.jpg";
}

created a missing_image.php with this the following code

<?php
$src = isset($_POST['src']) ? $_POST['src'] : NULL; 
if( is_null($src) ) exit; 

$file = file_get_contents('links.txt'); 

$pattern = '/<div[^>]+>\s<a[^>]+>\s<img src="'.preg_quote($src,'/').'"[^>]+>\s</a>\s</div>/'; 

if( preg_match($pattern,$file,$match) ) { 
   $newfile = str_replace($match[0],'',$file); 
   file_put_contents('links.txt',$newfile); 
}
?>

 

Still not working.. Any idea?

(Thanks to Derokorian for the codes)

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.