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

在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
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改