duanbenzan4050
2018-03-25 16:28
浏览 112
已采纳

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

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
] => )
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doulan0297 2018-03-26 18:19
    已采纳

    You can use the standard DOM library to parse 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);
    

    Output:

    Array
    (
        [Item Name] => BGCMA New Employee Hire
        [Category] => User Management
        [New Employee First Name] => Ike
        [New Employee Last Name] => Tester
    )
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongni8969 2018-03-26 14:44

    I used another HTML Parser and that solved my issue with converting the HTML table to a PHP array. It's called 'Table2arr' in case anyone else needs it. It's over 10 years old but it did the trick. https://www.phpclasses.org/package/3022-PHP-Parse-HTML-tables-and-extract-data-into-arrays.html

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题