note 57113 added to function.split
| From: | passtschuATfreenetDOTde at osu1 dot php dot net | Date: | Sat, 24 Sep 2005 07:09:20 +0000 |
| Subject: | note 57113 added to function.split | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-95679@lists.php.net to get a copy of this message | ||
divide a string with a template. the "template dividers" are the keys for the output
array.
<?PHP
function string2array ($string, $template){
#search defined dividers
preg_match_all ("|%(.+)%|U", $template, $template_matches);
#replace dividers with "real dividers"
$template = preg_replace ("|%(.+)%|U", "(.+)", $template);
#search matches
preg_match ("|" . $template . "|", $string, $string_matches);
#[template_match] => $string_match
foreach ($template_matches[1] as $key => $value){
$output[$value] = $string_matches[($key + 1)];
}
return $output;
}
$string1 = 'www.something.com 66.196.91.121 - - [01/Sep/2005:04:20:39 +0200] "GET
/robots.txt HTTP/1.0" 200 49 "-"';
$string2= '%Domain% %IP% - %User% \[%Date%:%Time% %TimeZone%\] "%Method% %Request%
%Protocol%" %ServerCode% %Bytes% "%Referer%"';
print_r (string2array ($string1, $string2));
/*
Array
(
[ServerAddress] => www.something.com
[IP] => 66.196.91.121
[User] => -
[Date] => 01/Sep/2005
[Time] => 04:20:39
[TimeZone] => +0200
[Method] => GET
[Request] => /robots.txt
[Protocol] => HTTP/1.0
[ServerCode] => 200
[Bytes] => 49
[Referer] => -
)
*/
?>
----
Manual Page -- http://www.php.net/manual/en/function.split.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57113
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57113&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57113&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57113&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57113&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57113&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57113&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57113&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57113&report=yes
Search -- http://master.php.net/manage/user-notes.php