Solved

regular expression modified to allow html

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
The viewer will learn how to count occurrences of each item in an array.
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 …

757 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

23 Experts available now in Live!

Get 1:1 Help Now