Solved

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

Posted on 2013-05-22
6
257 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
calculated column 12 74
How to use md5 hashing 3 28
How can I echo a PHP variable in AJAX function? 7 35
Selecting SUM data from mysql table with PHP 4 6
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…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 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

15 Experts available now in Live!

Get 1:1 Help Now