note 30430 modified in function.preg-split by aidan
| From: | aidan@php.net | Date: | Sat, 26 Jun 2004 06:26:42 +0000 |
| Subject: | note 30430 modified in function.preg-split by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71726@lists.php.net to get a copy of this message | ||
preg_split is very useful for splitting up the http common log. Sample:
<?php
$line = '10.0.0.2 - - [17/Mar/2003:18:03:08 +1100] "GET /images/org_background.gif
HTTP/1.0" 200 2321 "http://10.0.0.3/login.php" "Mozilla/5.0
Galeon/1.2.7 (X11; Linux i686; U;) Gecko/20021203"';
$elements = preg_split('/^(\S+) (\S+) (\S+) \[([^\]]+)\] "([^"]+)" (\S+) (\S+)
"([^"]+)" "([^"]+)"/', $line,-1,PREG_SPLIT_DELIM_CAPTURE |
PREG_SPLIT_NO_EMPTY);
print_r($elements);
?>
Results:
Array
(
[0] => 10.0.0.2
[1] => -
[2] => -
[3] => 17/Mar/2003:18:03:08 +1100
[4] => GET /images/org_background.gif HTTP/1.0
[5] => 200
[6] => 2321
[7] => http://10.0.0.3/login.php
[8] => Mozilla/5.0 Galeon/1.2.7 (X11; Linux i686; U;) Gecko/20021203
)
--was--
preg_split is very useful for splitting up the http common log. Sample:
$line = '10.0.0.2 - - [17/Mar/2003:18:03:08 +1100] "GET /images/org_background.gif
HTTP/1.0" 200 2321 "http://10.0.0.3/login.php" "Mozilla/5.0
Galeon/1.2.7 (X11; Linux i686; U;) Gecko/20021203"';
$elements = preg_split('/^(\S+) (\S+) (\S+) \[([^\]]+)\] "([^"]+)" (\S+)
(\S+) "([^"]+)" "([^"]+)"/', $line,-1,PREG_SPLIT_DELIM_CAPTURE |
PREG_SPLIT_NO_EMPTY);
print_r($elements);
Results:
Array
(
[0] => 10.0.0.2
[1] => -
[2] => -
[3] => 17/Mar/2003:18:03:08 +1100
[4] => GET /images/org_background.gif HTTP/1.0
[5] => 200
[6] => 2321
[7] => http://10.0.0.3/login.php
[8] => Mozilla/5.0 Galeon/1.2.7 (X11; Linux i686; U;) Gecko/20021203
)
http://php.net/manual/en/function.preg-split.php