Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Usuwanie linii w txt
-Robomon-
post
Post #1





Goście







Witam, mam plik tekstowy news.txt
Cytat
Tytuł2`del`05.02.2008 11:28:09`Tre�ć newsa2`Nick2
Tytuł1`yes`05.02.2008 11:28:00`Tre�ć newsa1`Nick1
Tytuł`del`05.02.2008 11:27:49`Tre�ć newsa`Nick


Potrzebuje taki skrypt, który po kliknięciu przycisku "Czyść", usunie wszystkie linie w których jest wartość "del". Próbowałem coś napisać ale nie bardzo mi to wychodzi ;/
Z góry thx za pomoc!
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 8)
nowotny
post
Post #2





Grupa: Zarejestrowani
Postów: 875
Pomógł: 122
Dołączył: 2.02.2008

Ostrzeżenie: (0%)
-----


Bardzo podobnie jak tu: http://forum.php.pl/index.php?showtopic=86...st&p=437393

Poza tym, pokaż co napisałeś... nikt za ciebie całej roboty odwalał nie będzie...
Go to the top of the page
+Quote Post
-Robomon-
post
Post #3





Goście







Cytat
Poza tym, pokaż co napisałeś... nikt za ciebie całej roboty odwalał nie będzie...
Wiem o tym doskonale ;p

  1. <?php
  2. $file = "news.txt";
  3. $contents= file($file);
  4. foreach($contents as $line) {
  5. $exp = explode("`",$value);
  6. $lol = 'del';
  7. if($exp[1]==$lol) {
  8. unset($exp[0]);
  9. unset($exp[1]);
  10. unset($exp[2]);
  11. unset($exp[3]);
  12. unset($exp[4]);
  13. $output.=implode('`',$exp);
  14. }
  15. }
  16. ?>
Go to the top of the page
+Quote Post
nowotny
post
Post #4





Grupa: Zarejestrowani
Postów: 875
Pomógł: 122
Dołączył: 2.02.2008

Ostrzeżenie: (0%)
-----


Na przykład tak:
  1. <?php
  2. $file = "news.txt";
  3. $contents= file($file);
  4. foreach($contents as $key=>$line) {
  5. $exp = explode("`",$line);
  6. $lol = 'del';
  7. if($exp[1]==$lol) {
  8. unset($contents[$key]);
  9. }
  10. }
  11. ?>


Ten post edytował nowotny 5.02.2008, 12:10:21
Go to the top of the page
+Quote Post
-Robomon-
post
Post #5





Goście







Stworzyłem stronę (del.php) z tym skryptem
czyli:
  1. <html><body>[php]<?php $file = "news.txt";
  2. $contents= file($file);
  3. foreach($contents as $key=>$line) {
  4. $exp = explode("`",$line);
  5. $lol = 'del';
  6. if($exp[1]==$lol) {
  7. unset($contents[$key]);
  8. }
  9. }
  10. ?>[/php]</body></html>


I domyślam się że po wejściu na nią powinno usunąć te linie?
Jeżeli tak to coś jest nie tak.. sad.gif

ps. thx za poświęcenie czasu
Go to the top of the page
+Quote Post
nowotny
post
Post #6





Grupa: Zarejestrowani
Postów: 875
Pomógł: 122
Dołączył: 2.02.2008

Ostrzeżenie: (0%)
-----


No nigdzie jeszcze nie zapisałeś zmian do pliku... jedynie usunąłeś te linie z tablicy $contents... teraz musisz zapisać zawartość tej tablicy do pliku...
Go to the top of the page
+Quote Post
-Robomon-
post
Post #7





Goście







  1. <?php
  2. $file = "news.txt";
  3. $contents= file($file);
  4. foreach($contents as $key=>$line) {
  5. $exp = explode("`",$line);
  6. $lol = 'del';
  7. if($exp[1]==$lol) {
  8. unset($contents[$key]);
  9.  $output.=implode('`',$exp);
  10. file_put_contents($file,$output);
  11. }
  12. }
  13. ?>


Hmm usuwa wszystkie linie poza tymi które mają "del"... co jest nie tak?
Go to the top of the page
+Quote Post
nowotny
post
Post #8





Grupa: Zarejestrowani
Postów: 875
Pomógł: 122
Dołączył: 2.02.2008

Ostrzeżenie: (0%)
-----


Zła funkcja w złym miejscu...
  1. <?php
  2. $file = "news.txt";
  3. $contents= file($file);
  4. $output='';
  5. foreach($contents as $key=>$line) {
  6. $exp = explode("`",$line);
  7. $lol = 'del';
  8. if($exp[1]==$lol) {
  9. unset($contents[$key]);
  10. }
  11. else{
  12. $output.=$line;
  13. }
  14. }
  15. file_put_contents($file,$output);
  16. ?>
Go to the top of the page
+Quote Post
-Robomon-
post
Post #9





Goście







Ok wielkie dzięki za pomoc! A właściwie za zrobienie tego winksmiley.jpg
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 02:39