Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Edycja plików tekstowych, Edytowanie i kasowanie fragmentów tekstu
Marteen
post
Post #1





Grupa: Zarejestrowani
Postów: 35
Pomógł: 2
Dołączył: 2.04.2010

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


Witam serdecznie.

Na swojej stronie nie korzystam z MySQL, więc opracowałem skrypt komentarzy oparty o pliki tekstowe. Polega on na tym, że do każdego elementu na stronie, który można skomentować (np. zdjęcie 5.jpg) tworzony jest w tym samym folderze na serwerze plik tekstowy o tej samej nazwie (np. 5.txt), który zawiera skrypt php deklarujący ponumerowane zmienne, których wartościami są kolejne komentarze. Oto przykładowy plik txt z komentarzami:
  1. $komentarz1 = "Przykładowy komentarz 1";
  2. $komentarz2 = "Przykładowy komentarz 2";
  3. $komentarz3 = "Przykładowy komentarz 3";


Z odczytywaniem ich nie mam problemu, lecz chciałbym dodać możliwość edytowania i kasowania pojedynczych komentarzy. Do tego potrzebuję wiedzieć, jak w php można edytować pliki tekstowe.

Powiedzmy, że chcę skasować z pliku linijkę skryptu, która deklaruje zmienną $komentarz2. Co należy zrobić? Jak edytować dowolny fragment pliku? Przyda mi się też sposób na obliczenie ilości wszystkich linijek w danym pliku. Proszę o pomoc.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
mlawnik
post
Post #2





Grupa: Zarejestrowani
Postów: 455
Pomógł: 49
Dołączył: 12.04.2010

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


Tak na szybko, z googla
  1. <?php
  2.  
  3. ###########################################################################
  4. ### ###
  5. ### This function takes two arguements, $fileName and $lineNum ###
  6. ### The example here shows how to delete line number 14 from the file ###
  7. ### myfile.txt ###
  8. ### Example: ###
  9. ### ###
  10. ### $fileName = "myfile.txt"; ###
  11. ### $lineNum = 14 ###
  12. ### delLineFromFile($fileName, $lineNum); ###
  13. ### Author Kevin Waterson kevin@phpro.org ###
  14. ### ###
  15. ###########################################################################
  16. // the file name, this can be a path also, like /path/to/myfile.txt
  17. $fileName = "myfile.txt";
  18.  
  19. // the line to delete
  20. $lineNum = 87;
  21.  
  22. delLineFromFile($fileName, $lineNum);
  23.  
  24.  
  25. function delLineFromFile($fileName, $lineNum){
  26. // check the file exists
  27. if(!is_writable($fileName))
  28. {
  29. // print an error
  30. print "The file $fileName is not writable";
  31. // exit the function
  32. }
  33. else
  34. {
  35. // read the file into an array
  36. $arr = file($fileName);
  37. }
  38.  
  39. // the line to delete is the line number minus 1, because arrays begin at zero
  40. $lineToDelete = $lineNum-1;
  41.  
  42. // check if the line to delete is greater than the length of the file
  43. if($lineToDelete > sizeof($arr))
  44. {
  45. // print an error
  46. print "You have chosen a line number, <b>[$lineNum]</b>, higher than the length of the file.";
  47. // exit the function
  48. }
  49.  
  50. //remove the line
  51. unset($arr["$lineToDelete"]);
  52.  
  53. // open the file for reading
  54. if (!$fp = fopen($fileName, 'w+'))
  55. {
  56. // print an error
  57. print "Cannot open file ($fileName)";
  58. // exit the function
  59. }
  60.  
  61. // if $fp is valid
  62. if($fp)
  63. {
  64. // write the array to the file
  65. foreach($arr as $line) { fwrite($fp,$line); }
  66.  
  67. // close the file
  68. fclose($fp);
  69. }
  70.  
  71. echo "done";
  72. }
  73.  
  74. ?>

Po angielsku, ale myślę, że sobie poradzisz
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 4.10.2025 - 22:21