Solved

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

Posted on 2013-05-22
6
259 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
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
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…

790 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