Solved

regex to get get 3 elements from a $string

Posted on 2013-05-22
2
272 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

863 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

26 Experts available now in Live!

Get 1:1 Help Now