Tutorials Infos - Anleitungen - Hilfe - Dreamcodes
 

Zeile löschen

Eine Zeile aus einer Textdatei löschen
 
Script:
<?
  
function deleteLineFromFile($filename, $text = "", $line = -1)
  {
     
$result = true;

     if (!
strlen ($text) && $line == -1)
       
$result = false;
     else
     {
        if (
file_exists($filename))
        {
           
$array = file($filename);

           
// Zeilennummer?
           
if ($line != -1)
           {
              if (
$line < count($array))
              {
                 unset(
$array[$line]);
                 
$array = array_values($array);
              }
              else
                
$result = false;
           }
           else
           {
              if (
strlen ($text))
              {
                 
$count = -1;

                 foreach (
$array as $value)
                 {
                    
$value = trim ($value);
                    if (
$text == $value)
                    {
                       
$count++;
                       unset(
$array[$count]);
                       
$array = array_values($array);
                       break;
                    }
                    else
                     
$count++;
                 }
              }
              else
                
$result = false;
           }
        }
        else
          
$result = false;
     }

     if (
$result = true)
     {
         
$fp = fopen ($filename, "w");

         if (
$fp)
         {
            foreach (
$array as $value)
              
fputs($fp, $value);

            
fclose ($fp);
         }
         else
           
$result = false;
     }

     return
$result;
  }

  
$fn = "test.txt";

  
// Loeschen einer Zeile mit Text-Angabe
  
deleteLineFromFile($fn, $text = "Hallo Welt----7", -1);

  
// Loeschen einer Zeile mit Zeilennummer
  
deleteLineFromFile($fn, $text = "", 3);
?>

 
ID: 1498
eingestellt am: 29.10.2007
Autor: Na
Status zum lesen: Gast
gelesen: 4913
Webseite: www.dreamcodes.com
[Drucken]