Solved

regex to get get 3 elements from a $string

Posted on 2013-05-22
2
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses four methods for overlaying images in a container on a web page
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 …

635 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