简体   繁体   English

从文本(字符串)中提取数据

[英]Extracting data from a text ( string )

I'am using email2http to post into my status file ! 我正在使用email2http发布到我的状态文件中!

When I receive the mail and get it posted to my status file , I was to extract some data and assign them to some values 当我收到邮件并将其发布到状态文件中时,我将提取一些数据并将其分配给某些值

I mean , I made that ! 我的意思是,我做到了!

<?php
$body = $_POST['body'];

function plog($errorMsg)
{
    $filename = "email.txt";
    if ($handle = fopen($filename, 'a+')) {
    if (fwrite($handle, $errorMsg) === FALSE) {
        echo "Cannot write to file ($filename)";
    }
}
else {
    echo "Cannot open file ($filename)";
}
fclose($handle);
}
$msgBody .= "$body\r\n";
plog($msgBody."\r\n");
?>

the $_POST['body'] contains the text sent from a mail $_POST['body']包含从邮件发送的文本

lets say all the mails come like this type : 可以说所有邮件都是这种类型的:

The amount of 5 USD has been deposited to your account. 已将5美元存入您的帐户。 Memo: Shopping Cart Payment. 备注:购物车付款。 Exchange ID :1317 . 交易所ID:1317。 Date: 23:07 25.07.13. 日期:23:07 25.07.13。 Batch: 28808853. 批次:28808853

So I want to get thos variables : 所以我想获取thos变量:

$amout=5; $ amout = 5;

$id=1317; $ id = 1317;

$date=23:07 25.07.13; $ date = 23:07 25.07.13;

$batch=28808853; $ batch = 28808853;

I think you can try to extract data with explode , if the mails are all time the same. 我认为您可以尝试使用explode提取数据,如果邮件都是相同的。

For example : 例如 :

$var = explode('amount of ', $_POST['body']);
$var2 = explode('USD', $var[1]);
$amount = $var2[0];

If the $body variable always has that structure you can do it with a regex: 如果$ body变量始终具有该结构,则可以使用正则表达式进行操作:

I would recommend to delete the \\r\\n. 我建议删除\\ r \\ n。

$body=str_replace(array("\r", "\n"), "", $body);
if(preg_match("/amount\sof\s(\d+)\s.*?Exchange\sID\s?:\s?(\d+?).*?Date:\s(\d{2}:\d{2}\s\d{2}\.\d{2}\.\d{2}).*?Batch:\s(\d+?)\./"), $body, $hits){
    print_r($hits);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM