Solved

regex to get get 3 elements from a $string

Posted on 2013-05-22
2
270 Views
Last Modified: 2013-05-22
[9/13/2011 6:36:41 PM] person1: Bob, this is AJ, please add me.
[9/14/2011 4:51:59 PM] person1: Hello
[9/15/2011 2:44:13 AM] person2: 
[1/6/2012 10:57:09 AM] person1: Bob, you there
[1/6/2012 10:57:11 AM] person1: It is AJ
[1/6/2012 10:57:34 AM] person2: hi AJ
[1/6/2012 11:00:56 AM] person1: Happy New Year
[1/6/2012 11:00:58 AM] person1: How are you doing
[1/6/2012 11:01:09 AM] person2: happy new year to you too

Open in new window


this is a skype chat log
can be
$string
or
input.txt

want
datetime,
name (person1, person2)
message
0
Comment
Question by:rgb192
2 Comments
 
LVL 6

Accepted Solution

by:
mickey159 earned 500 total points
ID: 39188564
$str="[9/13/2011 6:36:41 PM] person1: Bob, this is AJ, please add me.
[9/14/2011 4:51:59 PM] person1: Hello
[9/15/2011 2:44:13 AM] person2: 
[1/6/2012 10:57:09 AM] person1: Bob, you there
[1/6/2012 10:57:11 AM] person1: It is AJ
[1/6/2012 10:57:34 AM] person2: hi AJ
[1/6/2012 11:00:56 AM] person1: Happy New Year
[1/6/2012 11:00:58 AM] person1: How are you doing
[1/6/2012 11:01:09 AM] person2: happy new year to you too";

$a=explode("\n",$str);
foreach($a as $k=>$v){
preg_match("/([0-9:\/]+\s[0-9:]+\s[APM]+)\]\s(\w+)[:]{1}\s(.+)/",$v,$m);
$results[$k]["datetime"]=$m[1];
$results[$k]["name"]=$m[2];
$results[$k]["message"]=$m[3];
}

var_dump($results);

Open in new window


If you want a PHP formatted date, replace $m[1] with strtotime($m[1])
I hope this solve your problem.
0
 

Author Closing Comment

by:rgb192
ID: 39189934
this printed all the rows of the input
thanks
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now