Solved

regular expression modified to allow html

Posted on 2013-05-23
2
373 Views
Last Modified: 2013-05-23
$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
'; 



$a=explode("\n",$string);
foreach($a as $k=>$v){
preg_match("/([0-9:\/]+\s[0-9:]+\s[APM]+)\]\s(\w+)[:]{1}\s(.+)/",$v,$m);
$results[$k]["datetime"]=$m[1];
$results[$k]["name"]=$m[2];
$results[$k]["message"]=$m[3];
}
echo '<pre>';
var_dump($results);
echo '</pre>';

Open in new window


output
array(15) {
  [0]=>
  array(3) {
    ["datetime"]=>
    string(21) "4/21/2012 12:18:56 AM"
    ["name"]=>
    string(1) "J"
    ["message"]=>
    string(30) "you can send the $100/wk link
"
  }
  [1]=>
  array(3) {
    ["datetime"]=>
    string(21) "4/21/2012 12:22:00 AM"
    ["name"]=>
    string(2) "Me"
    ["message"]=>
    string(14) "or 150 or 200
"
  }
  [2]=>
  array(3) {
    ["datetime"]=>
    string(20) "4/24/2012 3:04:09 PM"
    ["name"]=>
    string(2) "Me"
    ["message"]=>
    string(18) "
"
  }
  [3]=>
  array(3) {
    ["datetime"]=>
    NULL
    ["name"]=>
    NULL
    ["message"]=>
    NULL
  }
  [4]=>
  array(3) {
    ["datetime"]=>
    NULL
    ["name"]=>
    NULL
    ["message"]=>
    NULL
  }
  [5]=>
  array(3) {
    ["datetime"]=>
    NULL
    ["name"]=>
    NULL
    ["message"]=>
    NULL
  }
  [6]=>
  array(3) {
    ["datetime"]=>
    NULL
    ["name"]=>
    NULL
    ["message"]=>
    NULL
  }
  [7]=>
  array(3) {
    ["datetime"]=>
    string(20) "4/24/2012 3:04:09 PM"
    ["name"]=>
    string(2) "Me"
    ["message"]=>
    string(18) "
"
  }
  [8]=>
  array(3) {
    ["datetime"]=>
    NULL
    ["name"]=>
    NULL
    ["message"]=>
    NULL
  }
  [9]=>
  array(3) {
    ["datetime"]=>
    NULL
    ["name"]=>
    NULL
    ["message"]=>
    NULL
  }
  [10]=>
  array(3) {
    ["datetime"]=>
    NULL
    ["name"]=>
    NULL
    ["message"]=>
    NULL
  }
  [11]=>
  array(3) {
    ["datetime"]=>
    NULL
    ["name"]=>
    NULL
    ["message"]=>
    NULL
  }
  [12]=>
  array(3) {
    ["datetime"]=>
    string(20) "4/24/2012 3:04:22 PM"
    ["name"]=>
    string(2) "Me"
    ["message"]=>
    string(111) "
"
  }
  [13]=>
  array(3) {
    ["datetime"]=>
    string(21) "4/24/2012 10:38:16 PM"
    ["name"]=>
    string(1) "J"
    ["message"]=>
    string(19) "sorry, i was tired
"
  }
  [14]=>
  array(3) {
    ["datetime"]=>
    NULL
    ["name"]=>
    NULL
    ["message"]=>
    NULL
  }
}

Open in new window


why doesnt this code grab the html messages
can the regular expression be modified to allow html
0
Comment
Question by:rgb192
2 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 39191796
>>why doesn't this code grab the html messages
Because on line 19 of your code you are splitting the data at newlines THEN your regex is examing data (one line at a time) that start with "[...]...".  

The "HTML" code appears on lines that do not start with "[...]" and thus never match.

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]);
}
unset($m);
echo '<pre>',htmlentities(print_r($results,true)),'</pre>';
exit;

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 39193400
works great with html

no errors

thanks
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 dynamically set the form action using jQuery.

808 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