Solved

regular expression modified to allow html

Posted on 2013-05-23
2
398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

691 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