Hello,
I can’t figure out the most effecient way to covert this:
Location: UK
Feature Program: No
Schedule: Active
Visible: Yes
Week: Weekday
Notes:
Happens on Friday. Usually.
to:
array('Location' => 'UK', 'Feature Program' => 'No', 'Schedule' => 'Active' ...)
I keep thinking that doing a couple explodes to break up the new lines then the colon-separated text while assigning the key or value to an array, but I was wondering if there’s a better way to tackle this.
JV1
October 7, 2011, 1:53am
2
If your list is always in the same format with the same labels, you can use this super complicated rocket science grade regex-foreach combination:
<?php
$str = 'Location: UK
Feature Program: No
Schedule: Active
Visible: Yes
Week: Weekday
Notes:
Happens on Friday. Usually.';
$pattern = "#((Location|Feature Program|Schedule|Visible|Week): (.*)\
|(Notes):[\\s\
]+(.*)$)#Uis";
preg_match_all($pattern, $str, $matches, PREG_SET_ORDER);
$result = array();
foreach($matches as $match)
{
$length = count($match);
$result[$match[$length-2]] = $match[$length-1];
}
var_dump($result);
?>
The output is:
array(6) {
["Location"]=>
string(2) "UK"
["Feature Program"]=>
string(2) "No"
["Schedule"]=>
string(6) "Active"
["Visible"]=>
string(3) "Yes"
["Week"]=>
string(7) "Weekday"
["Notes"]=>
string(27) "Happens on Friday. Usually."
}