Solved

regular expression modified to allow html

Posted on 2013-05-23
2
360 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

786 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