Solved

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

Posted on 2013-05-22
6
255 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
  • 3
  • 2
6 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Which datetime do you want?   Which names do you want?
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 84

Expert Comment

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

Accepted Solution

by:
hielo earned 500 total points
Comment Utility
>>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
Comment Utility
this works the pattern thanks
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now