Reputation: 45
I am trying to pull certain info from the string below. I’ve tried using explode (which worked) but it is a bit of a long winded procedure. I was wondering if there was a easier, logical way to do it?
An example of the string:
string(778) "Automatic reminders periodic maintenance schedule
You have a maintenance schedule
Vehicle: 357207058078957
Task: Service NA61 HNB
Rule: Every 10 mi or every 1 months after completion.
Task repeats when it is marked as completed
Last excuted:
Scheduled for: 23/11/2013 or 50720 mi
Due: since 45 d or in 50719 mi
Reminder: 22/11/2013 or 50715 mi
*** This is an automatically generated email, please do not reply. ***
If you have a question about our products and solutions, you can find your answer on our website under " frequently asked questions " or under "user guides ".
If you need to contact our Customer Support, please use our online contact form.
Kind regards"
I need to pull the following info from this example. This info won't always be the same, but the text before it (“Vehicle:”, etc.) will.
(Note: This is not real data.)
Other than that, the rest can be disregarded.
It doesn’t make a difference whether or not they still have the first bit (e.g Vehicle:), I can easily remove that bit myself.
Upvotes: 0
Views: 91
Reputation: 138
While Sam's solution is a lot cleaner, I just don't trust Regexpessions yet lol...
Anyways, I didn't see the reply here, but I went ahead and typed this out so I figured I'd post it anyways.
$msg ="Automatic reminders periodic maintenance schedule You have a maintenance schedule Vehicle: 357207058078957 Task: Service NA61 HNB Rule: Every 10 mi or every 1 months after completion. Task repeats when it is marked as completed Last excuted: Scheduled for: 23/11/2013 or 50720 mi Due: since 45 d or in 50719 mi Reminder: 22/11/2013 or 50715 mi *** This is an automatically generated email, please do not reply. *** If you have a question about our products and solutions, you can find your answer on our website under \" frequently asked questions \" or under \"user guides\". If you need to contact our Customer Support, please use our online contact form. Kind regards,";
$var = explode(' ',$msg);
$count = 0;
$array_count = 0;
$track_count = true;
$doc = array();
foreach ($var as $word){
switch ($word) {
case 'Vehicle:':
$doc[$array_count] = $var[$count].' '.$var[$count+1];
$array_count++;
break;
case 'Task:':
$doc[$array_count] = $var[$count].' ';
$count++;
for ($i=0; $i <3 ; $i++) {
$doc[$array_count] .= $var[$count+$i].' ';
}
$array_count++;
break;
case 'Rule:':
$while_count = 0; // run away
$doc[$array_count] = $word.' ';
while($var[$count].' '.$var[$count+1] != "Last excuted:" && $while_count < 30){
$doc[$array_count] .= $var[$count ].' ';
$while_count++;
$count++;
}
unset($while_count);
$array_count++;
$track_count = false;
break;
case 'Last':
if($var[$count].' '.$var[$count+1] == 'Last excuted:'){
$while_count = 0; // run away
$doc[$array_count] = $var[$count].' '.$var[$count+1];
$count = $count+2;
while($var[$count].' '.$var[$count+1] != 'Scheduled for:' && $while_count < 30){
$doc[$array_count] .= $var[$count].' ';
$while_count++;
$count++;
}
unset($while_count);
$array_count++;
break;
}
case 'Scheduled':
if($var[$count].' '.$var[$count+1] == 'Scheduled for:'){
$while_count = 0; // run away
$doc[$array_count] = $word.' '.$var[$count+1].' ';
$count = $count+2;
while($var[$count] != 'Due:' && $while_count < 30){
$doc[$array_count] .= $var[$count].' ';
$while_count++;
$count++;
}
unset($while_count);
break;
}
case 'Due:':
$while_count = 0; // run away
$doc[$array_count] = $word.' ';
$count++;
while($var[$count] != 'Reminder:' && $while_count < 30){
$doc[$array_count] .= $var[$count].' ';
$while_count++;
$count++;
}
unset($while_count);
$array_count++;
break;
case 'Reminder:':
$while_count = 0; // run away
$doc[$array_count] = $word.' ';
$count++;
while($var[$count] != '***' && $while_count < 30){
$doc[$array_count] .= $var[$count].' ';
$while_count++;
$count++;
}
unset($while_count);
break;
}
if( $count < count($var)-1 && $track_count ){$count++;}
}
echo '<pre>';var_dump($doc);echo"</pre>";
/*
array (size=6)
0 => string 'Vehicle: 357207058078957' (length=24)
1 => string 'Task: Service NA61 HNB ' (length=23)
2 => string 'Rule: Every 10 mi or every 1 months after completion. Task repeats when it is marked as completed ' (length=98)
3 => string 'Last excuted:' (length=13)
4 => string 'Due: since 45 d or in 50719 mi ' (length=31)
5 => string 'Reminder: 22/11/2013 or 50715 mi ' (length=33)
*/
Glad to see you found a solution
Upvotes: 0
Reputation: 20486
I cleaned everything up and the final RegEx I have is:
/Vehicle:(.*?)\nTask:(.*?)\nRule:(.*?)\nLast excuted:(.*?)\nScheduled for:(.*?)\nDue:(.*?)\nReminder:(.*?)\n\*/s
Breakdown:
Things in ()
are called match groups. So we look for Vehicle:
and then match .
(everything) for *
(0+ times). The ?
is necessary in here to make these lazy matches, not greedy matches..so .*
stops running when it hits the following character \nTask:
. This keeps going until the end, where we get everything up to the trailing \*
(an escaped *). Don't forget the /s
modifier at the ends which allows .
to match everything including newlines.
To implement that in PHP, you would do something as follows:
<?php
$string = <<<EOT
Automatic reminders periodic maintenance schedule
You have a maintenance schedule
Vehicle: 357207058078957
Task: Service NA61 HNB
Rule: Every 10 mi or every 1 months after completion.
Task repeats when it is marked as completed
Last excuted:
Scheduled for: 23/11/2013 or 50720 mi
Due: since 45 d or in 50719 mi
Reminder: 22/11/2013 or 50715 mi
*** This is an automatically generated email, please do not reply. ***
If you have a question about our products and solutions, you can find your answer on our website under " frequently asked questions " or under "user guides ".
If you need to contact our Customer Support, please use our online contact form.
Kind regards
EOT;
if(preg_match('/Vehicle:(.*?)\nTask:(.*?)\nRule:(.*?)\nLast excuted:(.*?)\nScheduled for:(.*?)\nDue:(.*?)\nReminder:(.*?)\n\*/s', $string, $matches)) {
unset($matches[0]); // $matches[0] contains the whole matches string
// Update the keys to something more logical
$keys = array('vehicle', 'task', 'rule', 'last_executed', 'scheduled_for', 'due', 'reminder');
$data = array_combine($keys, $matches);
// Trim the values, since we lazy selected in RegEx
// Note: you may want to do something more complicated, since `rule` still has whitespace
$data = array_map('trim', $data);
print_r($data);
// Array (
// [vehicle] => 357207058078957
// [task] => Service NA61 HNB
// [rule] => Every 10 mi or every 1 months after completion. Task repeats when it is marked as completed
// [last_executed] =>
// [scheduled_for] => 23/11/2013 or 50720 mi
// [due] => since 45 d or in 50719 mi
// [reminder] => 22/11/2013 or 50715 mi
// )
}
?>
To learn more read up on regular expressions on preg_match()
.
Upvotes: 1