Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

regex to get get 3 elements from a $string with html in 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

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
rgb192
Asked:
rgb192
  • 3
  • 2
1 Solution
 
ozoCommented:
Which datetime do you want?   Which names do you want?
0
 
ozoCommented:
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
 
rgb192Author Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ozoCommented:
What name, and what message do you want?
0
 
hieloCommented:
>>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
 
rgb192Author Commented:
this works the pattern thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now