Solved

regex to get get 3 elements from a $string with html in string

Posted on 2013-05-22
6
261 Views
Last Modified: 2013-05-23
[4/21/2012 12:18:56 AM] J: you can send the $100/wk link
[4/21/2012 12:22:00 AM] Me: or 150 or 200
[4/24/2012 3:04:09 PM] Me: <partlist alt="">
  <part identity="me">
    <name>me</name>
  </part>
</partlist>
[4/24/2012 3:04:09 PM] Me: <partlist alt="">
  <part identity="me">
    <name>me</name>
  </part>
</partlist>
[4/24/2012 3:04:22 PM] Me: <voicemail alt="Sent voice message to people in this conversation."><message length="4"></message></voicemail>
[4/24/2012 10:38:16 PM] J: sorry, i was tired

Open in new window



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

want
datetime,
name (person1, person2)
message

I have trouble parsing if there is html
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
  • 3
  • 2
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39190429
Which datetime do you want?   Which names do you want?
0
 
LVL 84

Expert Comment

by:ozo
ID: 39190492
Is this what you want?

$string='[4/21/2012 12:18:56 AM] J: you can send the $100/wk link
[4/21/2012 12:22:00 AM] Me: or 150 or 200
[4/24/2012 3:04:09 PM] Me: <partlist alt="">
  <part identity="me">
    <name>me</name>
  </part>
</partlist>
[4/24/2012 3:04:09 PM] Me: <partlist alt="">
  <part identity="me">
    <name>me</name>
  </part>
</partlist>
[4/24/2012 3:04:22 PM] Me: <voicemail alt="Sent voice message to people in this conversation."><message length="4"></message></voicemail>
[4/24/2012 10:38:16 PM] J: sorry, i was tired
';

preg_match ( '#\\[(.*?)\\][\\s\\S]*<name>(.*?)</name>[\\s\\S]*<name>(.*?)</name>[\\s\\S]*(<message.*?</message>)#' , $string, $matches );

echo $matches[1]."\n";
echo "name (".$matches[2].",". $matches[3].")\n";
echo $matches[4]."\n";
0
 

Author Comment

by:rgb192
ID: 39191048
output:
4/21/2012 12:18:56 AM name (me,me)

displays date for the first row but incorrect name and no message


want it to work for all rows


and just want for every row

datetime,
name
message
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 39192780
What name, and what message do you want?
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 39192975
>>What name, and what message do you want?
I believe he wants every "break" every segment that starts with "[DATE...]...".  From each of these segments he want the date, name, and what follows the name up to the beginning of the next segment.

@rgb192: Try:
preg_match_all('/([0-9:\/]+\s[0-9:]+\s[AP]M)\]\s(\w+)[:]\s([^\[]+)/',$string,$m);
$results=array();
foreach($m[0] as $k=>$v){
	$results[$k]=array("datetime"=>$m[1][$k], "name"=>$m[2][$k],"message"=>$m[3][$k]);
}
print_r($result);

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 39193398
this works the pattern 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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 dynamically set the form action using jQuery.

729 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