I made a function that actually finds and removes the <div>
from the targeted file.
This is the function:
function delete_div($file, $target, $replacement, $from, $to){
$side = file($file, FILE_IGNORE_NEW_LINES);
$reading = fopen($from, 'r');
$writing = fopen($to, 'w');
$replaced = false;
while (!feof($reading)) {
$line = fgets($reading);
if (stristr($line,$target)) {
$line = $replacement;
$replaced = true;
}
fputs($writing, $line);
}
fclose($reading); fclose($writing);
if ($replaced){
rename($to, $from);
} else {
unlink($to);
}
}
$file
is the file in which you want to remove the div.
$target
is the div or any other element you want to replace (in the $target
you have to specify what is in the div, the closing and opening tags and the spaces, aka. ' ').
$replacement
is what you want to replace the div with (in my case it's '' which means nothing).
$from
is to rename the file (in the process, everything from the file is put into a temporary file and after the div is replaced, the file gets renamed back into it's original name. I know I could have just used the $file
data instead of putting another parameter into the function, but I wrote it like that in case someone wants to rename the file after the div has been replaced).
$to
is the name of the temporary file (It doesn't matter what you name the temporary file as long as you put the extension after the filename, because the temporary file gets deleted after the div has been replaced)
So you could use the function like this:
delete_div('index.php', '<div id="linkovi1">text to be deleted 1</div>', '', 'index.php', 'rip.tmp');