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
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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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