Clean break.
am 31.07.2009 23:22:21 von Paul Halliday
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 "[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.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Clean break.
am 31.07.2009 23:35:48 von Phpster
On Jul 31, 2009, at 5:22 PM, 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
> "[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.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
Since it's a fixed length value, what about just using substr() to
pull out the various bits?
Bastien
Sent from my iPod
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Clean break.
am 31.07.2009 23:59:32 von Ollisso
On Sat, 01 Aug 2009 00:22:21 +0300, 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
>
....
Why not just use regexp ?
For example:
$string = "long text.. multiply lines...
[21/Jul/2009:00:00:47 -0300]
[ 1/Jul/2009:00:00:47 -0300]
[22/Jul/2009:00:00:47 -0300]";
preg_match_all('#\[([ 0-9a-zA-Z/]+):([0-9:]+)
[^]]+\]#',$string,$matches,PREG_SET_ORDER);
print_r($matches);
Output:
Array
(
[0] => Array
(
[0] => [21/Jul/2009:00:00:47 -0300]
[1] => 21/Jul/2009
[2] => 00:00:47
)
[1] => Array
(
[0] => [ 1/Jul/2009:00:00:47 -0300]
[1] => 1/Jul/2009
[2] => 00:00:47
)
[2] => Array
(
[0] => [22/Jul/2009:00:00:47 -0300]
[1] => 22/Jul/2009
[2] => 00:00:47
)
)
--
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: Clean break.
am 01.08.2009 00:37:56 von Paul Halliday
I was trying to stay away from regex as much as possible due to the
overhead? I might be wrong here.
This script will be parsing a lot of requests/sec. Thousands, maybe
more, which it also needs to toss into a DB. I want to try and keep it
as fast as possible. This is tricky when you don't know what you are
doing :). My coding is limited to hammering away at the search box on
php.net until I get a push in the right direction. It's just a hack
from there.
Using phpster's substr suggestion has already sped this up considerably.
2009/7/31 Ollisso :
> On Sat, 01 Aug 2009 00:22:21 +0300, Paul Halliday
m>
> wrote:
>
>> Whats the cleanest (I have a really ugly) way to break this:
>>
>> [21/Jul/2009:00:00:47 -0300]
>>
>> into:
>>
>> date=3D21/jul/2009
>> time=3D00:00:47
>>
> ...
> Why not just use regexp ?
>
> For example:
>
> $string =3D "long text.. multiply lines...
> =A0 =A0 =A0 =A0[21/Jul/2009:00:00:47 -0300]
>
> =A0 =A0 =A0 =A0[ 1/Jul/2009:00:00:47 -0300]
>
> =A0 =A0 =A0 =A0[22/Jul/2009:00:00:47 -0300]";
>
>
> preg_match_all('#\[([ 0-9a-zA-Z/]+):([0-9:]+)
> [^]]+\]#',$string,$matches,PREG_SET_ORDER);
>
> print_r($matches);
>
> Output:
> Array
> (
> =A0 =A0[0] =3D> Array
> =A0 =A0 =A0 =A0(
> =A0 =A0 =A0 =A0 =A0 =A0[0] =3D> [21/Jul/2009:00:00:47 -0300]
> =A0 =A0 =A0 =A0 =A0 =A0[1] =3D> 21/Jul/2009
> =A0 =A0 =A0 =A0 =A0 =A0[2] =3D> 00:00:47
> =A0 =A0 =A0 =A0)
>
> =A0 =A0[1] =3D> Array
> =A0 =A0 =A0 =A0(
> =A0 =A0 =A0 =A0 =A0 =A0[0] =3D> [ 1/Jul/2009:00:00:47 -0300]
> =A0 =A0 =A0 =A0 =A0 =A0[1] =3D> =A01/Jul/2009
> =A0 =A0 =A0 =A0 =A0 =A0[2] =3D> 00:00:47
> =A0 =A0 =A0 =A0)
>
> =A0 =A0[2] =3D> Array
> =A0 =A0 =A0 =A0(
> =A0 =A0 =A0 =A0 =A0 =A0[0] =3D> [22/Jul/2009:00:00:47 -0300]
> =A0 =A0 =A0 =A0 =A0 =A0[1] =3D> 22/Jul/2009
> =A0 =A0 =A0 =A0 =A0 =A0[2] =3D> 00:00:47
> =A0 =A0 =A0 =A0)
>
> )
>
>
> --
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: Clean break.
am 01.08.2009 20:12:00 von Ollisso
On Sat, 01 Aug 2009 01:37:56 +0300, Paul Halliday
wrote:
Actually, I don't think that regexp's are very slow :)
But I haven't tested them from that perspective.
You should just benchamrk different approaches :)
btw, not sure that substr will help you a lot:
first you have to get a begining of the time, block, then you have to find
next X chars.
Btw, you can also do it with a quite simple regexp:
preg_match_all('#\[(.{20})#', ..
(where 20 is amount of characters you want to extract.)
or:
preg_match_all('#\[(.{8})(.{12})#', ..
(where 8 is first part of date, and 12 is second part of date.)
But then you will have to parse this string, to convert it to time, which
could take time.
You should just benchmark different approaches, and check which is fastest.
PS: numbers 20,8, 12 are random, just put whtever you need :)
PPS: haven't tested regexps, just check the idea)
> I was trying to stay away from regex as much as possible due to the
> overhead? I might be wrong here.
>
> This script will be parsing a lot of requests/sec. Thousands, maybe
> more, which it also needs to toss into a DB. I want to try and keep it
> as fast as possible. This is tricky when you don't know what you are
> doing :). My coding is limited to hammering away at the search box on
> php.net until I get a push in the right direction. It's just a hack
> from there.
>
> Using phpster's substr suggestion has already sped this up considerably.
>
>
> 2009/7/31 Ollisso :
>> On Sat, 01 Aug 2009 00:22:21 +0300, 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
>>>
>> ...
>> Why not just use regexp ?
>>
>> For example:
>>
>> $string = "long text.. multiply lines...
>> [21/Jul/2009:00:00:47 -0300]
>>
>> [ 1/Jul/2009:00:00:47 -0300]
>>
>> [22/Jul/2009:00:00:47 -0300]";
>>
>>
>> preg_match_all('#\[([ 0-9a-zA-Z/]+):([0-9:]+)
>> [^]]+\]#',$string,$matches,PREG_SET_ORDER);
>>
>> print_r($matches);
>>
>> Output:
>> Array
>> (
>> [0] => Array
>> (
>> [0] => [21/Jul/2009:00:00:47 -0300]
>> [1] => 21/Jul/2009
>> [2] => 00:00:47
>> )
>>
>> [1] => Array
>> (
>> [0] => [ 1/Jul/2009:00:00:47 -0300]
>> [1] => 1/Jul/2009
>> [2] => 00:00:47
>> )
>>
>> [2] => Array
>> (
>> [0] => [22/Jul/2009:00:00:47 -0300]
>> [1] => 22/Jul/2009
>> [2] => 00:00:47
>> )
>>
>> )
>>
>>
>> --
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
>
>
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Clean break.
am 03.08.2009 11:24:24 von Peter Ford
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 "[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.
As far as I can tell from a brief test, date_create will happily parse your
format, so something like
$tmp = date_create($theParts[0]);
$theDate = $tmp->format("d/m/Y");
$theTime = $tmp->format("h:i:s");
should do it
--
Peter Ford phone: 01580 893333
Developer fax: 01580 893399
Justcroft International Ltd., Staplehurst, Kent
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Clean break.
am 04.08.2009 12:36:18 von LoneWolf
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 "[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
Re: Clean break.
am 04.08.2009 15:16:24 von Jerry Wilborn
--00163662e65785f425047050b1b5
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
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@gmail.com
On Tue, Aug 4, 2009 at 5:36 AM, Wolf 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
> "[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
>
>
--00163662e65785f425047050b1b5--
Re: Clean break.
am 04.08.2009 18:26:47 von List Manager
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@gmail.com
>
>
> On Tue, Aug 4, 2009 at 5:36 AM, Wolf 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
>> "[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.
$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