简体   繁体   中英

Extracting data from a text ( string )

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

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;

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];

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

I would recommend to delete the \\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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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