View Full Version : How would I explode this in PHP? :S
Recursion
01-06-2010, 04:34 PM
Hey,
I have some log files where I need to explode the lines into seperate cells in a table, Dan told me to use PHP explode(), which is fine, I'd just use a space as the delimiter, but towards the end of the line I get information that has spaces in I don't want to explode.
Example line:
2010.5.28 15:08:28 - 10.0.0.13 http://en.wikipedia.org/w/opensearch_desc.php *EXCEPTION* Exception site match. GET 884 0 1 200 - room4-1.domain.example -
(Ignore the hyphens, that's information Dansguardian hasn't been able to log).
As you can see, it's fine until I get to the "Exception site match." area.
Help please! xD
Apolva
01-06-2010, 04:45 PM
Which parts of the example do you want separated?
MattFr
02-06-2010, 12:34 PM
array explode ( string $delimiter , string $string [, int $limit ] )
Limit, see? Use space as the delimiter, limit to as far as you want to go.
Here ya go :)
http://habbcrazy.net/jack/thing.php
Source:
<?php
$string = "2010.5.28 15:08:28 - 10.0.0.13 http://en.wikipedia.org/w/opensearch_desc.php *EXCEPTION* Exception site match. GET 884 0 1 200 - room4-1.domain.example -";
preg_match( "/([0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}) - ([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}) (.*?) \*EXCEPTION\* Exception site match. (.*?) - (.*?) -/", $string, $matches );
echo "<p>";
print_r( $matches );
echo "</p>";
echo "<strong>Date/Time</strong>: {$matches['1']}<br />";
echo "<strong>IP</strong>: {$matches['2']}<br />";
echo "<strong>URL</strong>: {$matches['3']}<br />";
echo "<strong>GET</strong>: {$matches['4']}<br />";
echo "<strong>URL 2</strong>: {$matches['5']}<br />";
?>
I gave in with the validation at the end LOL
Jahova
03-06-2010, 07:33 AM
Nice for doing that ^^ +REP
MattFr
03-06-2010, 10:18 AM
Here ya go :)
http://habbcrazy.net/jack/thing.php
Source:
<?php
$string = "2010.5.28 15:08:28 - 10.0.0.13 http://en.wikipedia.org/w/opensearch_desc.php *EXCEPTION* Exception site match. GET 884 0 1 200 - room4-1.domain.example -";
preg_match( "/([0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}) - ([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}) (.*?) \*EXCEPTION\* Exception site match. (.*?) - (.*?) -/", $string, $matches );
echo "<p>";
print_r( $matches );
echo "</p>";
echo "<strong>Date/Time</strong>: {$matches['1']}<br />";
echo "<strong>IP</strong>: {$matches['2']}<br />";
echo "<strong>URL</strong>: {$matches['3']}<br />";
echo "<strong>GET</strong>: {$matches['4']}<br />";
echo "<strong>URL 2</strong>: {$matches['5']}<br />";
?>
I gave in with the validation at the end LOL
Bit OTT lol.
Bit OTT lol.
It's only a preg_match with regular expressions :P
Use them all the time :D
MattFr
03-06-2010, 10:39 AM
It's only a preg_match with regular expressions :P
Use them all the time :D
I know exactly what it is only :P
Meh, each to their own.
Source
03-06-2010, 03:49 PM
"Bit OTT lol."
Yes, let's all moan at someone for using common sense to create a method that isn't bodging it with an explode.
MattFr
03-06-2010, 04:08 PM
"Bit OTT lol."
Yes, let's all moan at someone for using common sense to create a method that isn't bodging it with an explode.
I wasn't moaning. A log viewer isn't something that needs to be efficient though, I think practically for the application; there's no point doing the regex for something that isn't important. See see?
Source
03-06-2010, 05:06 PM
No. Not at all.
If you're going to do it. Do it right, do it effectively, don't cut corners.
MattFr
03-06-2010, 05:21 PM
No. Not at all.
If you're going to do it. Do it right, do it effectively, don't cut corners.
As I said, there's no point spending too much time on something which isn't important. I spend a lot of time doing commissioned work properly, but I assume this isn't commissioned as he has to ask how to use explode, so there's no point spending a lot of time on it. Time management is an important part of being a successful developer and this situation certainly isn't one I would delegate a lot of time to.
Source
03-06-2010, 06:31 PM
I can guarantee for a well-skilled developer, doing regex over explode in this situation would be the same amount of time - if not quicker.
Your point of "this isn't commissioned as he has to ask how to use explode, so there's no point spending a lot of time on it.", holds no backing in my opinion. Explode would get the job done, but as he is clearly learning it would be better in the long run if he learns it (regex) now. Albeit not the best example.
He doesn't have to use it, but the option is there if he wants to.
I think efficiency is the most important. If you're working for Google, Facebook or Youtube who get thousands of connections every second, they want what's best for both the server and the client.
It's like Object-oriented PHP, it takes a while to learn but the output is a lot better.
MattFr
03-06-2010, 07:11 PM
I can guarantee for a well-skilled developer, doing regex over explode in this situation would be the same amount of time - if not quicker.
Your point of "this isn't commissioned as he has to ask how to use explode, so there's no point spending a lot of time on it.", holds no backing in my opinion. Explode would get the job done, but as he is clearly learning it would be better in the long run if he learns it (regex) now. Albeit not the best example.
He doesn't have to use it, but the option is there if he wants to.
Writing the regular expression over explode? Explode would be much faster.
It holds in my opinion. If I was developing something for my own personal use, efficiency wouldn't be a paramount concern for me, simply getting the code done would be much more important. Fair enough, if he wants to learn about it, using regex would be a good idea. However, personally, I would use explode if this wasn't commissioned, simply because of how quick using explode would be.
I think efficiency is the most important. If you're working for Google, Facebook or Youtube who get thousands of connections every second, they want what's best for both the server and the client.
It's like Object-oriented PHP, it takes a while to learn but the output is a lot better.
I never said efficiency wasn't important. I said efficiency isn't important for a small scale, personal script. Efficiency is one of the most important considerations when coding something that will be public, even if it's not a site with a high footfall. Yes, I understand about OOP considering I code Actionscript 3 which is entirely OO based. I'm not disagreeing with any of the things said in this thread, simply stating my opinion on how I would approach this if it was my project.
Dentafrice
08-06-2010, 12:18 PM
I wasn't moaning. A log viewer isn't something that needs to be efficient though, I think practically for the application; there's no point doing the regex for something that isn't important. See see?
You must have never worked with a large log before. When you start getting thousands, ten thousands, and hundred thousands of records.. you want to view and sort through them rather quickly.
A bodged down application, which has a flawed solution will only create more stress. It would be easier to match the string, then explode it instance by instance until you get what you want.
Explode isn't the best solution when it comes to this. If structure changes, your entire explode sequence would too. If it changed with a simple regex, one line would have to be configured.
Personal project or not, you should always code to long term time-saving, as well as efficiency.
I know I'd rather edit one / two lines of regex matching, then 7-8 lines of exploding and finding, then separation.
Whether it is just you, or an actual customer.. when it comes down to it in the future you will be glad you chose the right solution over the "fastest" solution for you to do. I've done it many times and regretted it when it came to modification / update time.
MattFr
08-06-2010, 01:16 PM
You must have never worked with a large log before. When you start getting thousands, ten thousands, and hundred thousands of records.. you want to view and sort through them rather quickly.
A bodged down application, which has a flawed solution will only create more stress. It would be easier to match the string, then explode it instance by instance until you get what you want.
Explode isn't the best solution when it comes to this. If structure changes, your entire explode sequence would too. If it changed with a simple regex, one line would have to be configured.
Personal project or not, you should always code to long term time-saving, as well as efficiency.
I know I'd rather edit one / two lines of regex matching, then 7-8 lines of exploding and finding, then separation.
Whether it is just you, or an actual customer.. when it comes down to it in the future you will be glad you chose the right solution over the "fastest" solution for you to do. I've done it many times and regretted it when it came to modification / update time.
Thanks for telling me I have never worked with a large log file. I made a questionnaire system for a client, parsing thousands of rows to a CSV file, all done with regex. I stand by what I said.
Dentafrice
08-06-2010, 02:18 PM
Then why would you not use the better method in an application like this? Are you saying you don't spend as much time implementing a good solution when it's work for your own self, or as you said small sites..?
If you used regex on your questionnaire system, there had to be a reason right? It worked better.. it was more efficient.. it got the job done right.. but yet on something of similar nature, that might not be as important.. or as popular.. you're saying implement a solution that will prove to be more work in the end, if it ever needs changing.. as well as pointlessly complex for what he needs.. correct?
Sounds like laziness.
Also, you call thousands of rows of questions a large log file? What about access logs, ever worked with them? They get quite large.. way larger then any questionnaire system ever would get. They require characteristics that other things do not.
MattFr
08-06-2010, 02:40 PM
Then why would you not use the better method in an application like this? Are you saying you don't spend as much time implementing a good solution when it's work for your own self, or as you said small sites..?
If you used regex on your questionnaire system, there had to be a reason right? It worked better.. it was more efficient.. it got the job done right.. but yet on something of similar nature, that might not be as important.. or as popular.. you're saying implement a solution that will prove to be more work in the end, if it ever needs changing.. as well as pointlessly complex for what he needs.. correct?
Sounds like laziness.
Also, you call thousands of rows of questions a large log file? What about access logs, ever worked with them? They get quite large.. way larger then any questionnaire system ever would get. They require characteristics that other things do not.
At the end of the day the way I code for my own personal things are the way I chose to do it. Yeah, I'm lazy when I'm coding for myself, that's because I've been coding for clients for too long and coding bores me, money is the only thing that inspires me to code, hence why I code ANY commissioned work in the most efficient way I can.
Source
08-06-2010, 03:52 PM
The issue comes down to the HxF community Caleb. People are extremely narrow minded and often mention about their client work in some attempt to *seem* professional.
My personal stance is that whether it is personal, or 'client' work you should always roll out the best possible code. Otherwise you get in to bad habits, look over things and start doing a generally poor job. It comes back down to 'regex wouldn't have taken that much longer than explode in this situation'.
It's a very bizarre stance one does have.
MattFr
08-06-2010, 04:20 PM
The issue comes down to the HxF community Caleb. People are extremely narrow minded and often mention about their client work in some attempt to *seem* professional.
My personal stance is that whether it is personal, or 'client' work you should always roll out the best possible code. Otherwise you get in to bad habits, look over things and start doing a generally poor job. It comes back down to 'regex wouldn't have taken that much longer than explode in this situation'.
It's a very bizarre stance one does have.
I'm not attempting to sound professional, or gain respect for anything, I don't need respect, approval or belief from anyone on this forum. The way I code is the way I code, sorry that my style differs from your obviously perfect style, but that's just the way it is! I know how to code properly when I need to and I always code properly and efficiently when commissioned. When coding for myself, for a non-public website, I cut corners to save time, simply because coding bores me.
I'm not narrow minded at all. I can accept that others have different style, as I have mine. The fact that we are now 20 posts into a thread, most of them critisizing my personal style of coding, suggests that others are narrow minded.
Quite simply, get over the fact that I do something differently to you.
Source
08-06-2010, 04:37 PM
I'm not being at all narrow minded. It's a simple based point of view I have, that people who are trying to learn languages should be taught the correct way and not some half-assed bodge job by some lazy ignorant user.
Your style, is your style. He asked for the best way to get his log reading done, and regex is the best way to achieve that.
MattFr
08-06-2010, 04:39 PM
I'm not being at all narrow minded. It's a simple based point of view I have, that people who are trying to learn languages should be taught the correct way and not some half-assed bodge job by some lazy ignorant user.
Your style, is your style. He asked for the best way to get his log reading done, and regex is the best way to achieve that.
He actually asked how he could explode it, I answered that.
Ignorant because I do something differently to you? My style is my style? Hm.
Apolva
08-06-2010, 04:40 PM
A little benchmarking...
<?php
$start = microtime(true);
for($i=0;$i<10000;$i++){ // Use explode on the string ten thousand times
$string="2010.5.28 15:08:28 - 10.0.0.13 http://en.wikipedia.org/w/opensearch_desc.php *EXCEPTION* Exception site match. GET 884 0 1 200 - room4-1.domain.example";
$bits=explode(" ",$string);
}
$stop = microtime(true);
echo "Time: ".($stop-$start)." seconds"
?>
On a typical shared webhost, Time: 0.0379159450531 seconds
We're talking micro-optimisation here, personally I too wouldn't bother (unless you have 300k+ lines), you don't look at a log very often.
MattFr
08-06-2010, 04:44 PM
A little benchmarking...
<?php
$start = microtime(true);
for($i=0;$i<10000;$i++){ // Use explode on the string ten thousand times
$string="2010.5.28 15:08:28 - 10.0.0.13 http://en.wikipedia.org/w/opensearch_desc.php *EXCEPTION* Exception site match. GET 884 0 1 200 - room4-1.domain.example";
$bits=explode(" ",$string);
}
$stop = microtime(true);
echo "Time: ".($stop-$start)." seconds"
?>
On a typical shared webhost, Time: 0.0379159450531 seconds
We're talking micro-optimisation here, personally I too wouldn't bother (unless you have 300k+ lines), you don't look at a log very often.
Haha. +rep if I can.
Source
08-06-2010, 04:46 PM
We're not on about speed here. We're on about the most low maintenance way to perform the task. Regex fullfils that.
MattFr
08-06-2010, 04:47 PM
We're not on about speed here. We're on about the most reliable way to perform the task. Regex fullfils that.
Please explain how regex is more reliable than explode?
Source
08-06-2010, 04:49 PM
The same way Dentafrice has mentioned already.
If the string/pattern you are trying to match changes, changing the regex pattern is *a lot* simpler then having to go through working out what segments are what.
Edit: People are skewing what I am trying to explain. There is nothing wrong with using explode, beyond the fact it's not the best way to achieve the result. I believe people who are asking for help should be informed correctly.
MattFr
08-06-2010, 04:53 PM
The likelihood of the data changing half way through your sample is extremely low, I don't really take that as a reason for choosing regex over explode for a personal project. Try to use the right words next time, might help your argument.
Source
08-06-2010, 04:58 PM
It's a completely valid argument.
Logs formats can easily change. Take the nginx log I have in front of me. It's 364,412 lines long and the format has recently changed as I installed the latest version a few months ago.
Apache2 logs could but don't change. Handling a 132mb file with explode would be a rediculous solution. However saying that, handling a file like that in PHP would be a stupid solution.
If I was to handle the viewing of that log with PHP, then changing the pattern would have been so much easier than re working the use of explode segments.
I don't, but it's a hypothetical situation.
MattFr
08-06-2010, 05:38 PM
Fair enough. People changing their log format is a bit shoddy.
eddiekins
15-06-2010, 11:32 AM
"Bit OTT lol."
Yes, let's all moan at someone for using common sense to create a method that isn't bodging it with an explode.
lulz i owe u rep for that.
Want to hide these adverts? Register an account for free!
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.