Script fejler perodisk?
Nu har jeg snart rodet med dette i tre dage, og tror nu jeg er kommet ned til kernen af problemet.... Jeg har bare ikke den fjerneste idé om hvad man kan gøre ved det.Det drejer sig om automatisk opdatering af en hjemmeside, hvor jeg;
1. ripper en anden hjemmeside. (Med Httrack webcopier)(Med tilladelse!)
2. Kører et script, som tager nogle oplysninger fra de downloadede filer.
3. Scriptet lægger disse filer i en tab separeret tekstfil, som jeg så direkte kan uploade til min hjemmeside....
Problemet er, at dette php script ikke får det hele med. Og selvom det er DET SAMME INPUT - får jeg ikke DET SAMME OUTPUT.
Det er forskelligt fra gang til gang hvor mange prods den lægger i tekstfilen.
Dette er noget skidt, og jeg håber virkelig at der er en der kan hjælpe!
Scriptet;
<?php
function extractData($filnavn) {
$pattern = '/.*<body(?:.*?<a class="fHierarchyParent".[^>]*>.[^<]*)?(?:.*?<a class="fHierarchyParent".[^>]*>(.[^<]*))?(?:.*?<a class="fHierarchyParent".[^>]*>(.[^<]*))?(?:.*?<img src=.[^{]*(.[^"]*))?.*<td valign="middle" class="fProductDescription"><b>(.[^<]*).*<td valign="middle" class="fProductNumber">(.[^<]*)(?:.*?<td width="40%" height="16" align="left" valign="middle" class="fProductDescription">.*?<td width="60%" height="16" align="left" valign="middle" class="fProductDescription">(.[^<]*))?(?:.*<td width="40%" height="16" align="left" valign="middle" class="fProductDescription">.*?<td width="60%" height="16" align="left" valign="middle" class="fProductDescription">(.[^<]*))?(?:.*<span class="prc">(.[^<]*))?(?:.*<a href="(.[^"]*).[^>]*>Mere info)?.*?<span class="fProductDescription">(.*)?<\/span>(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?(?:.*?<td width="250" valign="top" class="fProductProperties">(.[^<]*).*?<td width="320" valign="top" class="fProductProperties">(.*?)<\/td>)?.*$/';
// Kombinerer filen til en tekststreng
$string=file($filnavn);
$string2 = "";
for($i = 0; $i < count($string); $i++) {
$string2 .= rtrim($string[$i]);
}
// Kører og returnerer match
preg_match($pattern, $string2, $matches);
return $matches;
}
function writeFile($handle, $array) {
if ((isset($array[1])) && ($array[1] != "")) {
for($i = 1; $i < 10; $i++) {
if ($i == 1) fwrite($handle, "\"Hardware\"\t");
if ($i == 3) {
fwrite($handle, ($array[$i] == "" ? "\"\\N\"" : "upl/lowres/" . $array[$i]) . "\t");
fwrite($handle, ($array[$i] == "" ? "\"\\N\"" : "upl/mediumres/" . $array[$i]) . "\t");
fwrite($handle, ($array[$i] == "" ? "\"\\N\"" : "upl/" . $array[$i]) . "\t");
}
else {
if ($i == 8) fwrite($handle, ($array[$i] == "" ? "\"\\N\"" : str_replace(" ", "", str_replace(" ", "", $array[$i]))) . "\t");
else {
if ($i == 7)
if (($array[$i] == "Bestilt") || ($array[$i] == "")) fwrite($handle, "0" . "\t");
else fwrite($handle, "1" . "\t");
else {
if ($i == 9) fwrite($handle, ($array[$i] == "" ? "\"\\N\"" : "\"" . addslashes(str_replace("../../../external.html?link=", "", $array[$i])) . "\"") . "\t");
else fwrite($handle, ($array[$i] == "" ? "\"\\N\"" : "\"" . addslashes($array[$i]) . "\"") . "\t");
}
}
}
}
if ((isset($array[$i+1])) && ($array[$i+1] != "")) {
if ($array[$i] != "")
fwrite($handle, "\"" . addslashes($array[$i++]) . "<p>");
else {
fwrite($handle, "\"");
$i++;
}
while ($i < count($array)) {
fwrite($handle, addslashes($array[$i++]) . ": " . addslashes($array[$i++]) . "<br />");
}
fwrite($handle, "\"\n");
}
else {
if ($array[$i] != "")
fwrite($handle, "\"" . addslashes($array[$i++]) . "\"\n");
else fwrite($handle, "\"\\N\"\n");
}
}
}
// Åbner filen for skrivning
$handle1 = fopen("fiktiv.txt", "w");
// Skriver header
fwrite($handle1, "\"Hovedkategori\"\t\"Kategori\"\t\"Subkategori\"\t\"Image_low\"\t\"Image_medium\"\t\"Image_high\"\t\"Produktnavn\"\t\"Produktnummer\"\t\"Enhed\"\t\"Lagerstatus\"\t\"Pris uden moms\"\t\"Produktlink\"\t\"Produktbeskrivelse\"\n");
// Åbner det aktuelle bibliotek
$handle2 = opendir(getcwd());
// Løber igennem hver enkelt fil
while (false !== ($file = readdir($handle2))) {
if (substr($file,0,7) == "default") {
$array = extractData($file);
writeFile($handle1, $array);
}
}
// lukker det aktuelle bibliotek
closedir($handle2);
// lukker filen
fclose($handle1);
?>
