dqrdlqpo775594
dqrdlqpo775594
2013-07-26 09:32

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

已采纳

I'am using email2http to post into my status file !

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
";
plog($msgBody."
");
?>

the $_POST['body'] contains the text sent from a mail

lets say all the mails come like this type :

The amount of 5 USD has been deposited to your account. Memo: Shopping Cart Payment. Exchange ID :1317 . Date: 23:07 25.07.13. Batch: 28808853.

So I want to get thos variables :

$amout=5;

$id=1317;

$date=23:07 25.07.13;

$batch=28808853;

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongtun2459 dongtun2459 8年前

    I think you can try to extract data with explode, if the mails are all time the same.

    For example :

    $var = explode('amount of ', $_POST['body']);
    $var2 = explode('USD', $var[1]);
    $amount = $var2[0];
    
    点赞 评论 复制链接分享
  • duannaikuang1301 duannaikuang1301 8年前

    If the $body variable always has that structure you can do it with a regex:

    I would recommend to delete the .

    $body=str_replace(array("", "
    "), "", $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);
    }
    
    点赞 评论 复制链接分享

为你推荐