php 中特定电子邮件正文的键值对数组

key value pair array for specific email body in php

我有这样的电子邮件正文:

Title - Title Goes Here
Customer - Mr Abc Xyz
Terms And Conditions - You must accept our terms and conditions before sign in for any deals and 
offers.
You can refer our detailed information about this.

我已经使用 imap 来获取像 ['body']['html'] 这样的电子邮件正文,我想获取键值对数组,就像 codeigniter3

中的这样
Array(
       [Title] => Title Goes Here,
       [Customer] => Mr Abc Xyz,
       [Terms And Conditions] => You must accept our terms and conditions before sign in for any 
                                 deals and offers.You can refer our detailed information about this.
     )

我已尝试 explode() 获得超出预期的结果。

$arr = explode("-", $emailBodyContent);

但它给出了以下内容:

Array(
       [0] =>
       Title [1] => Title Goes Here,
       Customer [2] => Mr Abc Xyz,
       Terms And Conditions [3] => You must accept our terms and conditions before sign in for any 
                                 deals and offers.You can refer our detailed information about this.
     )

有人可以帮我吗?

由于您只是按 - 拆分它,因此您不会考虑不同的数据行。复杂的部分是最后一个条目看起来好像它可能有多行。

此代码首先按新行拆分,然后处理每一行并按 - 拆分。如果有 2 个部分 - 它会将它们添加为新项目,如果没有(如最后一位),它只会将内容添加到最后添加的条目中...

$emailBody = 'Title - Title Goes Here
Customer - Mr Abc Xyz
Terms And Conditions - You must accept our terms and conditions before sign in for any deals and 
offers.
You can refer our detailed information about this.';

$lines = explode("<br>", $emailBody);  
$output = [];
foreach ( $lines as $line ) {
    $lineSplit = explode("-", $line, 2);
    if ( count($lineSplit) == 2 ) {
        $lastKey = trim($lineSplit[0]);
        $output [ $lastKey ] = trim($lineSplit[1]);
    }
    else    {
        $output [ $lastKey ] .= " ".trim($line);
    }
}

print_r($output);

给...

Array
(
    [Title] => Title Goes Here
    [Customer] => Mr Abc Xyz
    [Terms And Conditions] => You must accept our terms and conditions before sign in for any deals and offers. You can refer our detailed information about this.
)