Jump to content

Delete text from a txt-file


Nals

Recommended Posts

Hello everyone,

 

This is my first post and I need a little help. My knowledge of php is limited, so this question may look simple. English isn't my mother tongue, which you have probably noticed already.

 

So, my problem:

I 've made a a phpscript that adds text to a txt-file, that works perfect, but I also need something to delete some parts of the file.

 

 

part of my html-code:

<form method="POST" action="verwijderscript.php">
<table class="noborder">
<tr><td class="noborder">

<input type="text" id="verwijdertitel" name="verwijdertitel" size="40">
<input type="submit" name="s1" value="Verwijder Bericht" />

</td></tr>
</table>
</form>

 

part of my txt-file:

<div  id="Testtitel">
<span class="subtitel">Testtitel</span>
<span class="rechts">Gepost op 15/03/2011</span>
<br>
<img class="nieuws" src="image.gif"><br>
texttexttexttexttext<br>
texttexttext<br>
text

<div class="streep"></div>
</div id="Testtitel">

<div  id="Testtitel2">
<span class="subtitel">Testtitel2</span>
<span class="rechts">Gepost op 15/03/2011</span>
<br>
<img class="nieuws" src="image2.gif"><br>
texttexttexttexttext<br>
texttexttext<br>
text

<div class="streep"></div>
</div id="Testtitel2">

 

part of my php-code:

<?php

$titel = $_POST['verwijdertitel'];
$oud = file_get_contents("data.txt");
$data = fopen("data.txt", "w+");
$nieuw =  str_replace("id=\""$titel, "id=\"hidden", $oud);


fwrite($data, "$nieuw");
fclose($data);
            header("refresh:0;url=http://www.test.com");

?>

 

css:

#hidden{display:none;}

 

Problem: parse error line 6 of php-file

And I don't think this works if the error is solved. I don't have a MySQL database, that's why I use a txt files.

 

What I try to do is to hide the div, it would be better to delete it, but than I need to work with regex, I guess 

 

Link to comment
https://forums.phpfreaks.com/topic/230717-delete-text-from-a-txt-file/
Share on other sites

You have an extra quote and a missing quote in the search arg:

 

$oud = file_get_contents("data.txt");
$nieuw =  str_replace("id=\"$titel", "id=\"hidden", $oud);
file_put_contents($nieuw, "data.txt");
header("refresh:0;url=http://www.test.com");

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.