如何为大于199的数字创建preg_match?

时间:2015-06-26 13:19:11

标签: php regex http-post preg-match

我有一个用PHP编码的拍卖物品'offer form'。我想创建一个preg_match字符串,如果输入此表单的任何货币值小于199英镑,则返回错误。该公式应允许特殊字符£,$,€和字母表中的所有字母与要约值一起输入。所以这个公式将接受/允许以下输入 - 199或199英镑或199克或199英镑或199美元或199英镑或欧元199等。

到目前为止,我的当前字符串仅限制输入的值,如果低于'100',并且如果输入值,则不包括符号和字母的容差。

如果你们中的任何人可以帮助我,我将非常感激?这是我到目前为止的代码 -

} elseif (!preg_match('/^[-+]?[\p{Nd} .-]{3,30}$/', ($_POST['offer']))) {

$error_msg .= "Please enter an offer above £199 GBP! \n";

根据需要更新了此帖子,以显示更多脚本 -

    // end score assignments
    # was there a reCAPTCHA response?
if (isset($_POST["recaptcha_response_field"])) {
        $resp = recaptcha_check_answer ($privatekey,
                                        $_SERVER["REMOTE_ADDR"],
                                        $_POST["recaptcha_challenge_field"],
                                        $_POST["recaptcha_response_field"]);

unset($_POST["recaptcha_challenge_field"]);
unset($_POST["recaptcha_response_field"]);

        if (!$resp->is_valid) {
                # set the error code so that we can display it
                $error_msg .= $resp->error;
               }

}
    if (empty($_POST['name']) || empty($_POST['address']) || empty($_POST['postcode']) || empty($_POST['email']) || empty($_POST['mobile']) || empty($_POST['item']) || empty($_POST['offer'])) {
        $error_msg .= "Oops you've missed something out! - Please complete ALL (*) required fields.. \n";
        } elseif (!preg_match('/^[-+]?[\p{Nd} .-]{11,30}$/', ($_POST['mobile']))) {
                $error_msg .= "Please enter a valid mobile number! \n";
        } elseif (!preg_match('/^\s*(£|\$|GBP|Euro|gbp)?\s*(199|([2-9]\d\d+))$)|(^\s*(199|([2-9]\d\d+)\s*(£|\$|GBP|Euro|gbp)?)$/', ($_POST['offer']))) {
                $error_msg .= "&#149; Please enter an offer above £199 GBP!<br />&#149; Enter only numbers, no letters or symbols.<br /> \n";
    } elseif (strlen($_POST['name']) > 25) {
        $error_msg .= "The name field is limited at 25 characters. Your first initial and surname will do! \n";
    } elseif (!ereg("^[A-Za-z' -]*$", $_POST['name'])) {
        $error_msg .= "The name field must not contain special characters. \n";
    } elseif (!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$",strtolower($_POST['email']))) {
        $error_msg .= "Please enter a valid e-mail address! \n";
    } elseif (!empty($_POST['url']) && !preg_match('/^(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i', $_POST['url'])) {
                $error_msg .= "Invalid website url.";

}

    if ($error_msg == NULL && $points <= $maxPoints) {
        $subject = $_REQUEST['domain'];

        $message = "You received an offer through your website: \n\n";
        foreach ($_POST as $key => $val) {
            $message .= ucwords($key) . ": $val \r\n";
        }


        if (strstr($_SERVER['SERVER_SOFTWARE'], "Win")) {
            $headers   = "From: $yourEmail \r\n";
            $headers  .= "Reply-To: {$_POST['email']}";
        } else {
            $headers   = "From: $yourWebsite <$yourEmail> \r\n";
            $headers  .= "Reply-To: {$_POST['email']}";
        }

        if (mail($yourEmail,$subject,$message,$headers)) {

$selectedProjects  = 'None';
if(isset($_POST['projects']) && is_array($_POST['projects']) && count($_POST['projects']) > 0){
    $selectedProjects = implode(', ', $_POST['projects']);
}

$body .= 'Selected Projects: ' . $selectedProjects;

    $url = 'success.htm';
    echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">'; 

echo '<p><span style="font-family:Arial;font-size:14px;font-style:normal;font-weight:bold;color: green;">Your offer has been sent to us successfully, thank you!</p>

<p><span style="font-family:Arial;font-size:14px;font-style:normal;font-weight:bold;color: green;">We will be in touch with you shortly.</span> </p>';

        } else {
            echo '<p><span style="font-family:Arial;font-size:14px;font-style:normal;font-weight:bold;color: green;">Your mail could not be sent this time!</p>';
        }
    }
}
function get_data($var) {
    if (isset($_POST[$var]))
        echo htmlspecialchars($_POST[$var]);
}
if ($error_msg != NULL) {
    echo '<p><strong style="color: red;">ERROR:</strong><br />';
    echo nl2br($error_msg) . "</p>";
}
?>

<script type="text/javascript">
var RecaptchaOptions = {    
theme : 'clean' 
}; 
</script>

1 个答案:

答案 0 :(得分:3)

您可以使用:

(?:^\s*(?:£|\$|GBP|Euro|gbp)?\s*(?:199|1\d{3}\d*|(?:[2-9]\d\d+))$)|(?:^\s*(?:(?:199|1\d{3}\d*|(?:[2-9]\d\d+))\s*(?:£|\$|GBP|Euro|gbp)?)$)

DEMO

Regular expression visualization 示例代码:

   else if (!preg_match('/(^\s*(?:£|\$|GBP|Euro|gbp)?\s*(?:199|1\d{3}\d*|(?:[2-9]\d\d+))$)|(?:^\s*(?:(?:199|1\d{3}\d*|(?:[2-9]\d\d+))\s*(?:£|\$|GBP|Euro|gbp)?)$)$)/', $_POST['offer'])) {
           $error_msg .= "&#149; Please enter an offer above £199 GBP!<br />&#149; Enter only numbers, no letters or symbols.<br /> \n";
    }