You are here: Re: Optimizing a string manipulation script. « PHP Programming Language « IT news, forums, messages
Re: Optimizing a string manipulation script.

Posted by Alan Little on 06/08/06 01:00

Carved in mystic runes upon the very living rock, the last words of
<Cleverbum@hotmail.com> of comp.lang.php make plain:

> Alan Little wrote:
>
>> Carved in mystic runes upon the very living rock, the last words of
>> <Cleverbum@hotmail.com> of comp.lang.php make plain:
>>
>> > I'm not really accustomed to string manipulation and so I was
>> > wondering if any of you could be any help i speeding up this script
>> > intended to change the format of some saved log information into a
>> > CSV file while removing duplicate records.
>> > The main problem is that the script currently takes about 20
>> > seconds to execute, and were it to take much longer it would time
>> > out.
>> >
>> > Below is the script itself, and then some example lines from the
>> > log file it processes:
>> >
>> > <?
>> > [snip]
>> > ?>
>>
>> Whew!
>>
>> How about an example of the output you're trying to achieve? That
>> might be easier.
>
> Here we go then:

Try this:

<?php
$patt =
'!([^:]+:) \[([^:]+:\d\d:\d\d:\d\d) [+-](\d{4})\] '.
'(\d+\.\d+\.\d+\.\d+) (-) (-) "(\w+) (/[^ ]*) '.
'(HTTP/\d\.\d)" (\d+) (\d+) "([^"]+)" "([^"]+)"'.
"\n?".'!';

$log = fopen('log.csv', 'a');

$logfile = file_get_contents('logs.txt');
$logfile = ereg_replace("\r\n?", "\n", $logfile);

preg_match_all($patt, $x, $matches, PREG_SET_ORDER);

foreach($matches as $match) {
unset($match[0]);
$logline = implode(',', $match);
fputs($log, $logline."\n");
}

fclose($log);
?>

I don't know what those two blank log elements are after the IP, so this
pattern will only work when they're blank.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация