在php中将多行字符串列表转换为多个变量

时间:2018-03-25 16:28:44

标签: php list arraylist

我是一个PHP菜鸟。我有一个多行列表,并希望将每一行转换为它自己的变量。基本上,我希望变量与左侧的名称相同,变量的值作为右侧的列。中间列有一个':'。我试图看看我是否可以使用列表并爆炸,但我不知道该怎么做。这就是列表的样子。我需要它是动态的,因为列表中的值会改变。

我将下面的列表存储为$ list

  

项目名称:新雇员雇用

     

类别:用户管理

     

新员工名:Ike

     

新员工姓氏:Tester

     

显示名称:IT Tester

     

职位名称:Admin

     

电话:333-333-3333

我试过

list($itemname, $category, $firstname, $lastname, $displayname, $title, $phone) = explode(":", $list);

list($itemname, $category, $firstname, $lastname, $displayname, $title, $phone) = explode("/n", $list);

更新。如果您需要/想要查看原始代码。

$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

更新#2 - 使用简单的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('\n', $line);
$variables[trim($key)] = trim($value);
unset($key, $value);
}

这是print_r和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
] => )

2 个答案:

答案 0 :(得分:0)

我使用了另一个HTML Parser,解决了将HTML表转换为PHP数组的问题。它被称为'Table2arr'以防其他人需要它。它已经超过10年了,但它确实做到了。 https://www.phpclasses.org/package/3022-PHP-Parse-HTML-tables-and-extract-data-into-arrays.html

答案 1 :(得分:0)

您可以使用标准DOM库来解析HTML:

<?php

$html = "<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>";

$data = array();
$lastKey = null;
$dom = new DOMDocument();
$dom->loadHTML($html);
$tdElements = $dom->getElementsByTagName('td');
foreach ($tdElements as $i => $element) {
    if ($i % 3 == 0) {
        $lastKey = trim($element->textContent);
    } else if ($i % 3 == 2) {
        $data[$lastKey] = trim($element->textContent);
    }
}

print_r($data);

输出:

Array
(
    [Item Name] => BGCMA New Employee Hire
    [Category] => User Management
    [New Employee First Name] => Ike
    [New Employee Last Name] => Tester
)