0

I have a huge logfile to parse through with PHP that looks for something like this (please note that variables that are variable will be labelled):

16:09:47.925 T#10648 PresenceManager: ࿚noticing[specialchar]$name[specialchar]0x8fac711e4bf14e62-d-s111.221.74.46:40022-r[IP]:48914-l192.168.1.2:48914f2812a403bdc6ade

I want to be able to look for this line that contains $name, then parse out the [IP] part, which is an IP. Please note there's two special char places which I have marked that cannot be shown in the post.

This is what I have:

if(preg_match('/' . $name . '*?-r(\d+\.\d+\.\d+\.\d+)/', $contents, $results))

However it doesn't seem to be finding the given ip like above D:

Pastebinny:: http://pastebin.com/YHh4fndP

$log = https://mega.co.nz/#!Scc11A6K!RXziJU_Ii43o1gcQetEfS7Kfzt-bY7VTJXljpCS7Gfc (username is sliceteam)

Thanks!

6
  • Working on it now, sorry but I'm a little confused by "two special char places which I have marked that cannot be shown in the post". Commented Apr 5, 2014 at 6:42
  • It's hard to explain, the characters are a failure of decoding the message :3, couldn't something with the "dot" be used in its stead? Commented Apr 5, 2014 at 6:45
  • One more question, can you say what you consider the "IP" in your given example? Do you want the 111.221.74.46 string (i.e. the first set of 4 digits) or do you want the string after [IP]:? Commented Apr 5, 2014 at 6:47
  • Sorry for the confusion, the 111 ip also changes, but is not the same as the [IP], I want to capture [IP]. The 111 changes, so you may need to develop the regex for this bit too, as it is not the same. Thanks for the help! Commented Apr 5, 2014 at 6:49
  • You want to just capture the data after [IP] if the string contains data from a $name variable? Can we assume that the string [IP] will only exist once on the line or that it's always the last instance? Commented Apr 5, 2014 at 6:55

1 Answer 1

1

Now that I understand how the IP is found, try this regex:

/(?:variable.*?-r)((?:\d{1,3}\.){3}\d{1,3})/

variable is obviously where you would include the $name variable. This uses a non capturing group to look for variable followed by any characters up to -r (the IP's preceder), and then capture an IP-like string. I defined an IP as 3 sets of 1-3 digits followed by a period followed by one final set of 1-3 digits.

I hate to say it, but it seems like there is a different error in your script. I narrowed your code down to:

<?php
$name = 'sliceteam';
$log = (array_pop(glob('debug-20140405-1732.log')));
$contents = file_get_contents($log);
if (preg_match_all("/(?:$name.*?-r)((?:\d{1,3}\.){3}\d{1,3})/", $contents, $results))
{
    echo json_encode(array('status' => 'success', 'success' => $results[1]));
}
?>

And it returned {"status":"success","success":["168.62.23.92","213.146.168.254"]} (which seems pretty damn right to me ;)`). What do you receive when you run the entire script..and I can try to debug the problem with you.

Sign up to request clarification or add additional context in comments.

18 Comments

Uhm, I'm sorry for not specifying properly. T_T. I didn't explain this well in the OP. I replaced a real ip address as [IP], the [IP] isn't the actual string I wanted to get out, it represents a valid ip address.
@user3500524, that makes sense, my bad for not realizing. A quick question, I see the string 111.221.74.46 between $name and [IP] in your example..how do I know that that isn't the IP (it looks like an IP to me)? In other words, I think we need to specify some more rules before I can right that regex.
Aaaaaaah - having read through the comments here - I now actually understand what you're after! Probably simplest if I just remove my answer as this one has gotta be pretty close now...
@Sam Jim Beam and RegExp ... that can't be a good match! (baduuum tssssssss) .... sorry ;)
Alright, I'm going to upload the actual log, please open it in Notepad++ to see the special chars, I won't be able to copy it properly. The Log contains a lot of crap I don't need xD. The username in this example is "sliceteam" mega.co.nz/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.