Solved

regular expression modified to allow html

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Force PDF to open inline as opposed to dowload 16 37
PHP 5.6 and 7.x 4 39
A responsive image gallery using flexbox 6 22
Get data from two MySQL tables 6 27
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…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

733 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