Solved

regex to get get 3 elements from a $string

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

770 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