用于解析大于设定数量的每个值的HTML电子邮件的PHP脚本

时间:2012-06-28 16:22:24

标签: php parsing email

我有一个PHP脚本解析的订单确认html电子邮件。该脚本成功完成了一些事情但我无法弄清楚如何通过电子邮件搜索所有产品价格,然后设置一个标志,如果该值大于500英镑。

基本上,如果订单超过500英镑,我希望脚本向我发送电子邮件提醒。

以下是我到目前为止的情况。 $ body是HTML电子邮件的内容。我知道那部分是有效的,因为其他规则设置得很好。我认为我的问题是preg_match的$ pattern规则 - 我认为它没有正确识别价格值。

有没有办法可以搜索$ body以获取格式为£xx.xx(其中每个x是一个数字)的任何内容,并为每个找到的值大于500英镑的值添加1到$ temp?我无法解决这个问题......

if (stristr($body,'Order Confirmation') !== false) {

    $string = $body;

    $pattern = '/^[0-9]{1,}$/';

    preg_match ($pattern, $string, $matches);

    $temp =  array_filter($matches, function($value) {
        return $value >= 500;
    });

if ($temp >= 1)
{
    $headers = "Content-type: text/html \r\n";
    $headers .= "From: Robot<robot@robot.com>";
    mail('joe@blogs.com','Internet Order Value Over £500', $body, $headers);
}

}

1 个答案:

答案 0 :(得分:0)

我认为这会做你想要的。如果您使用php扩展名复制并粘贴到文件中,代码应该可以正常工作。您必须为脚本略微编辑它。

<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" media="screen" href="bla.css" >
    <script type="text/javascript" src="https:ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>

<body>

<?php

$string = "lots of things here oh look a price in bold <b>£656.00</b> then more things oh wow";

$pattern = '~<b>£([^<]*).([^<]*)</b>~'; //pattern to find the price in-between the <b></b> tags in $string

preg_match ($pattern, $string, $matches);

$price = substr($matches[0],5); // removes the <b>£ bits from the match if present

if ($price >= 500.00) // if statement to determine if greater than a value
{
echo "value is greater than £500";
    }

?>

</body>
</html>