I'm a php noob. I have a multi-line list and would like to convert each line to it's own variable. Basically, I would want the variable to be the same name as on the left and the value for the variable to be the column on the right. The middle column has a ':'. I'm trying to see if i can use list and explode but I'm not sure how to do it. This is what the list looks like. I need it to be dynamic because the values in the list will change.
I have the list below stored as $list
Item Name : New Employee Hire
Category : User Management
New Employee First Name : Ike
New Employee Last Name : Tester
Display Name : IT Tester
Job Title : Admin
Phone : 333-333-3333
I've tried
list($itemname, $category, $firstname, $lastname, $displayname, $title, $phone) = explode(":", $list);
list($itemname, $category, $firstname, $lastname, $displayname, $title, $phone) = explode("/n", $list);
UPdate. In case you needed/wanted to see the original code.
$data = "<table style='margin:0px;padding:0px;'>
<tr>
<td style='text-align:left; vertical-align:top;'>
Item Name
</td><td style='vertical-align:top;'> : </td>
<td style='text-align:left; vertical-align:top;'>
BGCMA New Employee Hire
</td></tr><tr>
<td style='text-align:left; vertical-align:top;'>
Category
</td><td style='vertical-align:top;'> : </td>
<td style='text-align:left; vertical-align:top;'>
User Management
</td></tr><tr>
<td style='text-align:left; vertical-align:top;'>
New Employee First Name
</td><td style='vertical-align:top;'> : </td>
<td style='text-align:left; vertical-align:top;'>
Ike
</td></tr><tr>
<td style='text-align:left; vertical-align:top;'>
New Employee Last Name
</td><td style='vertical-align:top;'> : </td>
<td style='text-align:left; vertical-align:top;'>
Tester
</td></tr>
</table>";
$list = htmlspecialchars_decode(stripslashes($data)); //this shows list
Update #2 - Using Simple HTML Dom Parser
$html = str_get_html("$data");
$list = $html; //this shows list
$variables = [];
$lines = explode(" : ", $list);
foreach ($lines as $line) {
if (empty(trim($line))) {
continue; //Skip empty lines
}
list($key, $value) = explode('
', $line);
$variables[trim($key)] = trim($value);
unset($key, $value);
}
This is the print_r and var_dump
var_dump($variables);
array(5) { ["
Item Name "]=> string(0) "" [" BGCMA New Employee Hire
Category "]=> string(0) "" [" User Management
New Employee First Name "]=> string(0) "" [" Ike
New Employee Last Name "]=> string(0) "" [" Tester
"]=> string(0) "" }
print_r($variables)
Array ( [
Item Name ] => [ BGCMA New Employee Hire
Category ] => [ User Management
New Employee First Name ] => [ Ike
New Employee Last Name ] => [ Tester
] => )