Re: Clean break.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jerry Wilborn wrote:
> Am I missing something? Can't this be done quickly/easily with preg_match()?
> 
> if (preg_match('/\[(.*):(.*)\s/U', '[21/Jul/2009:00:00:47 -0300]',
> $matches)) {
>         print "date: {$matches[1]}, time: {$matches[2]}";
> }
> 
> Jerry Wilborn
> jerrywilborn@xxxxxxxxx
> 
> 
> On Tue, Aug 4, 2009 at 5:36 AM, Wolf <LoneWolf@xxxxxxxxx> wrote:
> 
>> Paul Halliday wrote:
>>> Whats the cleanest (I have a really ugly) way to break this:
>>>
>>> [21/Jul/2009:00:00:47 -0300]
>>>
>>> into:
>>>
>>> date=21/jul/2009
>>> time=00:00:47
>>>
>>> Caveats:
>>>
>>> 1) if the day is < 10 the beginning of the string will look like
>> "[<space>1/...
>>> 2) the "-0300" will differ depending on DST or TZ. I don't need it
>>> though, it just happens to be there.
>>>
>>> This is what I have (it works unless day < 10):
>>>
>>> $theParts = split("[\"]", $theCLF);
>>>
>>>         // IP and date/time
>>>         $tmpParts = explode(" ", $theParts[0]);
>>>         $theIP = $tmpParts[0];
>>>         $x = explode(":", $tmpParts[3]);
>>>         $theDate = str_replace("[","", $x[0]);
>>>         $theTime = "$x[1]:$x[2]:$x[3]";
>>>
>>> the full text for this part looks like:
>>>
>>> 10.0.0.1 - - [21/Jul/2009:00:00:47 -0300] ... more stuff here
>>>
>>> Anyway, any help would be appreciated.
>>>
>>> thanks.
>>>
>> unset ($STRING,$pos,$pos2,$pos3,$L,$D,$T,$pos4,$NString);
>> $STRING="10.0.0.1 - - [21/Jul/2009:00:00:47 -0300] ... more stuff here"
>> $pos=strpos($STRING,"[");
>> $pos2=strpos($STRING,"]");
>> $L=$pos2-$pos;
>> $NString=substr($STRING,$pos,$L);
>> $pos3=strpos($NString,":");
>> $D=substr($NString,0,$pos3);
>> $pos4=$pos3++;
>> $T=substr($NString,$pos4,8);
>> echo "date=$D";
>> echo "time=$T";
>>
>> untested, but that should be pretty much all you need.
>>
>> HTH,
>> Wolf
>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
> 

How about this.

<?php

$regex = '^.*[^\[]+\[([a-zA-Z0-9\/]+):([0-9:]+) ([0-9-]+)\].*$';

$in = '10.0.0.1 - - [21/Jul/2009:00:00:47 -0300] ... more stuff here';

if ( preg_match('%'.$regex.'%', $in, $m) ) {
    print "date: {$m[1]}, time: {$m[2]}";
}

?>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux