Seite 2 von 3

Verfasst: 02.04.2006 15:45
von Dennis63
Dann machste ne WHILE Schleife und machst alles manuell mit substr() und strpos()

Grüße
Dennis

Verfasst: 02.04.2006 15:46
von fanrpg

Code: Alles auswählen

while( list( $id, $text) = each( $array) )
{
$text = stripslashes($text);
if( !empty($text) or $text = " " )
{
// Ausführen wenn Zeile nicht leer ist
}
}
So könnte man das umgehen würde ich dann mal sagen.
Zeile für Zeile.

Oder so wie Dennis geschrieben hat würds auch gehen.

Verfasst: 02.04.2006 18:07
von mgutt
Derzeitiger Stand (Problem besteht immer noch):

Code: Alles auswählen

<?php
$datei = "dateiname.htm";
$text = file($datei);
for ($i = 0; $i < count($text); $i++)
{
 	if( empty($text[$i]) || $text[$i] == " " || $text[$i] == "" || $text[$i] == " /n" || $text[$i] == "/n") 
	{
		unset($text[$i]);
	}
	else
	{
		$text[$i] = preg_replace("/\<(.*?)\>/si", "", $text[$i]);
		$text[$i] = str_replace("&nbsp;", "", $text[$i]);
		$text[$i] = str_replace("<!--", "", $text[$i]);
		$text[$i] = str_replace("var thispagename = self.location.href.substring(self.location.href.lastIndexOf('/')+1,self.location.href.length);", "", $text[$i]);
		$text[$i] = str_replace("if(!parent.mainFrame)", "", $text[$i]);
		$text[$i] = str_replace("{", "", $text[$i]);
		$text[$i] = str_replace("}", "", $text[$i]);
		$text[$i] = str_replace('window.location.href="index.html?"+thispagename;', "", $text[$i]);
		$text[$i] = str_replace("//-->", "", $text[$i]);
		$text[$i] = trim($text[$i]);
	}
}
print_r($text);
?>

Verfasst: 02.04.2006 18:11
von S2B
Warum liest du die Datei eigentlich nicht mit fread() in einen String und bearbeitest den dann mit RegEx's oder str_replace()? Dann sparst du dir nämlich die ganzen Schleifen und hast auch kein Problem mehr mit trim()...

Verfasst: 02.04.2006 19:45
von mgutt
ich brauche einen array, da die zeile x der titel ist und der text nachher die zeile y. (als beispiel) und die werte kommen dann nachher in die datenbank

ich verstehe das nicht.

warum gibt das:

Code: Alles auswählen

<?php
$datei = "dateiname.htm";
$text = file($datei);
for ($i = 0; $i < count($text); $i++)
{
	$text[$i] = preg_replace("/\<(.*?)\>/si", "", $text[$i]);
	$text[$i] = str_replace("&nbsp;", "", $text[$i]);
	$text[$i] = str_replace("<!--", "", $text[$i]);
	$text[$i] = str_replace("var thispagename = self.location.href.substring(self.location.href.lastIndexOf('/')+1,self.location.href.length);", "", $text[$i]);
	$text[$i] = str_replace("if(!parent.mainFrame)", "", $text[$i]);
	$text[$i] = str_replace("{", "", $text[$i]);
	$text[$i] = str_replace("}", "", $text[$i]);
	$text[$i] = str_replace('window.location.href="index.html?"+thispagename;', "", $text[$i]);
	$text[$i] = str_replace("//-->", "", $text[$i]);
	$text[$i] = trim($text[$i]);
 	if ( empty($text[$i]) || $text[$i] == " " || $text[$i] == "" || $text[$i] == " /n" || $text[$i] == "/n") 
	{
		unset($text[$i]);
	}
}
print_r($text);
?>
plötzlich das aus:

Code: Alles auswählen

Array
(
    [36] =>     </tr>

    [37] =>     <tr> 

    [38] =>       <td width="9%"> <div align="left"></div></td>

    [39] =>       <td width="82%"> <div align="center"> 

    [40] =>           <p><strong>titel</strong></p>

    [41] =>         </div></td>

    [42] =>       <td width="9%">&nbsp;</td>

    [43] =>     </tr>

    [44] =>     <tr>

    [45] =>       <td height="30">&nbsp;</td>

    [46] =>       <td height="30">&nbsp;</td>

    [47] =>       <td height="30">&nbsp;</td>

    [48] =>     </tr>

    [49] =>     <tr> 

    [50] =>       <td height="30">&nbsp;</td>

    [51] =>       <td><div align="center"> 

    [52] =>           <p>textzeile

    [53] =>             textzeile

    [54] =>             textzeile

    [55] =>             textzeile 

    [56] =>             textzeile 

    [57] =>             textzeile

    [58] =>             textzeile</p>

    [59] =>           </div></td>

    [60] =>       <td height="30">&nbsp;</td>

    [61] =>     </tr>

    [62] =>     <tr> 

    [63] =>       <td width="9%" height="30">&nbsp;</td>

    [64] =>       <td width="82%" height="30"> <div align="right"><font face="Arial, Helvetica, sans-serif"><a href="lexikon.htm">zur&uuml;ck</a></font></div></td>

    [65] =>       <td width="9%" height="30">&nbsp;</td>

    [66] =>     </tr>

    [67] =>   </table>

    [68] => </div>

    [69] => </body>

    [70] => </html>

)
bis einschließlich 35 schien es ja geklappt zu haben.. danach greift der filter aber nicht mehr und gibt den normalen inhalt aus?!

zeile 35 beinhaltet das:

Code: Alles auswählen

      <td colspan="3">&nbsp;</td>

Verfasst: 02.04.2006 20:23
von mgutt
Ok, der Fehler war naheliegend. Bei For-Schleifen wird der 2. Ausdruck in der Klammer immer wieder aufs neue geprüft und durch das Löschen einzelnder Array Elemente hat sich die Anzahl der Zeilen reduziert, was einen Abbruch der Schleife zur Folge hatte, bevor das Letzte eigentlich erreicht wurde.

Daher muss ich die Zeilenanzahl einfach vorher einmal zählen, bevor ich mit der Schleife beginne.

So sieht es jetzt aus:

Code: Alles auswählen

<?php
$datei = "dateiname.htm";
$text = file($datei);
$textzeilen = count($text);
for ($i = 0; $i < $textzeilen; $i++)
{
	$text[$i] = preg_replace("/\<(.*?)\>/si", "", $text[$i]);
	$text[$i] = str_replace("&nbsp;", "", $text[$i]);
	$text[$i] = str_replace("<!--", "", $text[$i]);
	$text[$i] = str_replace("var thispagename = self.location.href.substring(self.location.href.lastIndexOf('/')+1,self.location.href.length);", "", $text[$i]);
	$text[$i] = str_replace("if(!parent.mainFrame)", "", $text[$i]);
	$text[$i] = str_replace("{", "", $text[$i]);
	$text[$i] = str_replace("}", "", $text[$i]);
	$text[$i] = str_replace('window.location.href="index.html?"+thispagename;', "", $text[$i]);
	$text[$i] = str_replace("//-->", "", $text[$i]);
	$text[$i] = trim($text[$i]);
 	if ( empty($text[$i]) ) 
	{
		unset($text[$i]);
	}
}
$text = array_values($text);
print_r($text);
?>
am ende sortiere ich mit array_values die array ids neu und alles ist so wie es soll :D

Verfasst: 02.04.2006 20:56
von hagily
Postest du bitte mal wenn du das ganze fertig hast?

Felix

Verfasst: 04.04.2006 00:03
von mgutt

Code: Alles auswählen

<?php

$aktuell = getcwd(); 

$verzeichnis = opendir($aktuell); 

while ($eintrag = readdir())
{
	if (is_file($eintrag) && strrchr($eintrag, ".") == '.htm' )
	{
		$text = file($eintrag);
		$textzeilen = count($text);
		for ($i = 0; $i < $textzeilen; $i++)
		{
			$text[$i] = str_replace(">zur&uuml;ck<", "><", $text[$i]); // Diverse Filter für Tags, Zeilen und Wörter, die wir nicht gebrauchen können.
			$text[$i] = preg_replace("/\<(.*?)\>/si", "", $text[$i]);
			$text[$i] = str_replace("&nbsp;", "", $text[$i]);
			$text[$i] = str_replace("<!--", "", $text[$i]);
			$text[$i] = str_replace("var thispagename = self.location.href.substring(self.location.href.lastIndexOf('/')+1,self.location.href.length);", "", $text[$i]);
			$text[$i] = str_replace("if(!parent.mainFrame)", "", $text[$i]);
			$text[$i] = str_replace("{", "", $text[$i]);
			$text[$i] = str_replace("}", "", $text[$i]);
			$text[$i] = str_replace('window.location.href="index.html?"+thispagename;', "", $text[$i]);
			$text[$i] = str_replace("//-->", "", $text[$i]);
			$text[$i] = trim($text[$i]);
			if ( empty($text[$i]) ) 
			{
				unset($text[$i]);
			}
		}
		$text = array_values($text);
		$titel = addslashes($text[0]);
		$beschreibung = addslashes(trim(str_replace($titel, '', implode(" ", $text))));
		if ( !empty($titel) && !empty($beschreibung) && strpos($titel, 'Adobe Web-Fotogalerie') === false ) // Hier werden die Dateien gefiltert, die nicht zum Lexikon hinzugefügt werden sollen. Am besten vor dem SQL erst alle Titel und Beschreibungen per Echo ausgeben
		{
			$sql  = "INSERT INTO ". LEXIKON ." (titel,text) VALUES ('$titel','$beschreibung')";
			if ( !mysql_query($sql) )
			{
				die("Message: Could not update lexikon information <br> at File: " . __FILE__ . " on line: " . __LINE__);
			}
		}
	}
}

closedir($verzeichnis);

?>
hat wunderbar geklappt.

Verfasst: 05.04.2006 13:23
von hagily
Ich habe nun eine Datei mit nicht nur 2 sondern 6 Spalten (Name Altersgruppe Beitrag Termin Verfügbar? ID)

wie muss ich jetzt $text und $titel verändern/erweitern damit das passt?

Verfasst: 05.04.2006 14:23
von mgutt
ersetz mal das:

Code: Alles auswählen

      $titel = addslashes($text[0]); 
      $beschreibung = addslashes(trim(str_replace($titel, '', implode(" ", $text)))); 
      if ( !empty($titel) && !empty($beschreibung) && strpos($titel, 'Adobe Web-Fotogalerie') === false ) // Hier werden die Dateien gefiltert, die nicht zum Lexikon hinzugefügt werden sollen. Am besten vor dem SQL erst alle Titel und Beschreibungen per Echo ausgeben 
      { 
         $sql  = "INSERT INTO ". LEXIKON ." (titel,text) VALUES ('$titel','$beschreibung')"; 
         if ( !mysql_query($sql) ) 
         { 
            die("Message: Could not update lexikon information <br> at File: " . __FILE__ . " on line: " . __LINE__); 
         } 
      }
gegen das um zu schauen was überhaupt für inhalte da sind:

Code: Alles auswählen

      print_r($text);