---
title: "Preg_match_all"
type: "forum-thread"
url: "https://www.webforum.nu/amne/php/85496-preg-match-all"
topic: "PHP"
topic_url: "https://www.webforum.nu/amne/php"
author: "ZeUs"
published: "2003-09-07T23:05:33.000Z"
updated: "2003-09-11T16:14:20.000Z"
replies: 2
views: 198
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/php/85496-preg-match-all"
---

# Preg_match_all

## #1 — ZeUs, 2003-09-07T23:05Z

Tjenare..
Jag sitter med ett script som jag kodade före sommaren. Det används för att extrahera ut vårt schema från <http://schema.hj.se/visa.asp?from=2003-09-08&tom=&prog=Dm3+++&kurs=&lokal=&larare=&visap=Nej&bolag=ING>  för att senare kunna använda det i exempelvis Outlook/PDA.
Funkar hyfsat men det är en sak som brister... När det är flera lektioner samma tid på samma dag så plockar scriptet bara ut den första lektionen.
så här ser koden ut...

```
<?php
$datum = date("Y-m-d");
$url = "http://schema.hj.se/visa.asp?from=$datum&tom=&prog=Dm3+++&kurs=&lokal=&larare=&visap=Nej&bolag=ING";
function getPageCode($url){
	$data=0;
	if(!($fp = fopen ($url, "r")))
	{
		die("Cannot connect to: $url");
	}

	while ($in = fread($fp, 4096))
	{
		$data .= $in;
	}

	return $data;
}

$host = "localhost";
$user = "Andreas";
$password = "";
$dbname = "schema";
$link = mysql_connect ($host, $user, $password);
$sql2="DELETE FROM `schema` " or die(mysql_error());
mysql_db_query ($dbname, $sql2, $link);
mysql_close ($link);
$link2 = mysql_connect ($host, $user, $password);
$code = getPageCode($url);
preg_match_all("/(<form>\s*.*<+form>\s)/mis", $code, $matches);
$nystrang = $matches[1][0];

preg_match_all("/\s*<TD VALIGN=\"TOP\" ALIGN=\"LEFT\" class=\"brodtext\">(\d*-\d*-\d*)&nbsp;\s(\w*)&nbsp;<\/TD>\s*<TD VALIGN=\"TOP\" class=\"brodtext\">(\d+:\d+)-(\d+:\d+)&nbsp;<\/TD>\s*<TD VALIGN=\"TOP\" class=\"brodtext\">(\w*)\s*&nbsp;<\/TD>\s*<TD VALIGN=\"TOP\" class=\"brodtext\">(\w*)\s*&nbsp;<\/TD>\s*<TD VALIGN=\"TOP\" class=\"brodtext\">([A-Za-z\s]*)&nbsp;<\/TD>\s*<TD VALIGN=\"TOP\" class=\"brodtext\">(\w\d\d\d)\s*[A-Za-z-]*\s*&nbsp;<\/TD>\s*<TD VALIGN=\"TOP\" class=\"brodtext\">(\w*)&nbsp;<\/TD>\s*<TD VALIGN=\"TOP\" class=\"brodtext\">([\w\s\/]*)&nbsp;<\/TD>\s*<TD VALIGN=\"TOP\" class=\"brodtext\">\s(\w*)&nbsp;\s*<\/TD>/mis", $nystrang, $matches2);
for ($i=0; $i< count($matches2[1]); $i++) {
       $sql="INSERT INTO schema(datum, dag, start, slut, program, kurs, grupp, sal, sign, moment) VALUES ('".$matches2[1][$i]."','".$matches2[2][$i]."','".$matches2[3][$i]."','".$matches2[4][$i]."','".$matches2[5][$i]."','".$matches2[6][$i]."','".$matches2[7][$i]."','".$matches2[8][$i]."','".$matches2[9][$i]."','".$matches2[10][$i]."')" or die(mysql_error());
        mysql_db_query($dbname, $sql, $link2) or die(mysql_error()); 
	}
//}
mysql_close($link2);
?>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
informationen har sparats...
</body>
</html>
```

Koden från schema sidan är strukturerat uppbyggd:

```
<form>
      <TD VALIGN="TOP" ALIGN="LEFT" class="brodtext">2003-09-10&nbsp; Ons&nbsp;</TD>
      <TD VALIGN="TOP" class="brodtext">13:00-16:45&nbsp;</TD>
      <TD VALIGN="TOP" class="brodtext">Dm3   &nbsp;</TD>
      <TD VALIGN="TOP" class="brodtext">Datgraf &nbsp;</TD>
      <TD VALIGN="TOP" class="brodtext">1  &nbsp;</TD>
      <TD VALIGN="TOP" class="brodtext">E440  
&nbsp;</TD>
      <TD VALIGN="TOP" class="brodtext">RuAn&nbsp;</TD>
      <TD VALIGN="TOP" class="brodtext">Laboration
      &nbsp;</TD>
      <TD VALIGN="TOP" class="brodtext"> &nbsp; 
</TD>
</form>
```

Snälla kan någon hjälpa mig?
Kan det vara någon  preg_match_all egenskap eller liknande som jag missat?

Permalänk: https://www.webforum.nu/p/85496

## #2 — Matte, 2003-09-08T22:56Z

Tror snarare det är så att du inte får med de rader där Grp består av en siffra.

Testa det här (om nu inget rensas bort när jag sparar inlägget)

```php
preg_match_all("/\\s*<TD VALIGN=\"TOP\" ALIGN=\"LEFT\" class=\"brodtext\">(\\d*-\\d*-\\d*)&nbsp;\\s(\\w*)&nbsp;<\\/TD>\\s*<TD VALIGN=\"TOP\" class=\"brodtext\">(\\d+:\\d+)-(\\d+:\\d+)&nbsp;<\\/TD>\\s*<TD VALIGN=\"TOP\" class=\"brodtext\">(\\w*)\\s*&nbsp;<\\/TD>\\s*<TD VALIGN=\"TOP\" class=\"brodtext\">(\\w*)\\s*&nbsp;<\\/TD>\\s*<TD VALIGN=\"TOP\" class=\"brodtext\">(\\w*)\\s*&nbsp;<\\/TD>\\s*<TD VALIGN=\"TOP\" class=\"brodtext\">(\\w\\d\\d\\d)\\s*[A-Za-z-]*\\s*&nbsp;<\\/TD>\\s*<TD VALIGN=\"TOP\" class=\"brodtext\">(\\w*)&nbsp;<\\/TD>\\s*<TD VALIGN=\"TOP\" class=\"brodtext\">([\\w\\s\\/]*)&nbsp;<\\/TD>\\s*<TD VALIGN=\"TOP\" class=\"brodtext\">\\s(\\w*)&nbsp;\\s*<\\/TD>/is", $nystrang, $matches2);
```

Permalänk: https://www.webforum.nu/p/1153550

## #3 — ZeUs, 2003-09-11T16:14Z

Mycket riktigt!
Nu fungerar det... Tack så mycket för hjälpen

Permalänk: https://www.webforum.nu/p/1155010

---

Tråden på webben: https://www.webforum.nu/amne/php/85496-preg-match-all
