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条)

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?