php中特定电子邮件正文的键值对数组
我的电子邮件正文如下:php中特定电子邮件正文的键值对数组,php,codeigniter,codeigniter-3,Php,Codeigniter,Codeigniter 3,我的电子邮件正文如下: 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
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.
)
有人能帮我吗?由于您只是将其按
-
进行拆分,因此不会考虑不同的数据行。复杂的部分是最后一个条目看起来可能有多行
此代码首先将其拆分为新行,然后处理每行并按-
将其拆分。如果有两个部分-它会将它们作为新项目添加,如果没有(如最后一位),它只会将内容添加到最后添加的条目中
$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);
消息:未定义变量:lastKey
即将到来数组([0]=>标题-标题在此显示客户-Mr Abc Xyz条款和条件-您必须在登录任何交易和优惠之前接受我们的条款和条件。您可以参考我们关于此的详细信息。[1]=>)
和行数得到2正如您最初建议的,我只使用了“\n”。我还尝试使用“\r\n”和变量lastkey
作用域仅限于if条件,因此上述错误导致最终输出为Array([Title]=>标题位于此处客户-Abc Xyz先生条款和条件-您必须在登录任何交易和优惠之前接受我们的条款和条件。您可以参考我们的详细信息。)
。因此,它只创建一个键是的,它在每行后面都包含
。
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.
)