![](/img/trans.png)
[英]Is there a simple way to use FILTER_SANITIZE_NUMBER_INT to validate a phone number (in a form)?
[英]Editing form to sanitize/validate phone number
我在PHP方面的經驗非常有限,我真的希望有人可以幫助我。
我想做的是清理/驗證輸入的電話號碼,以便只允許輸入號碼。
我想我需要使用FILTER_SANITIZE_NUMBER_INT
但不確定在哪里或如何使用它。
這是我的代碼:
<?php
// Replace the email address with the one that should receive the contact form inquiries.
define('TO_EMAIL', '########');
$aErrors = array();
$aResults = array();
/* Functions */
function stripslashes_if_required($sContent) {
if(get_magic_quotes_gpc()) {
return stripslashes($sContent);
} else {
return $sContent;
}
}
function get_current_url_path() {
$sPageUrl = "http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
$count = strlen(basename($sPageUrl));
$sPagePath = substr($sPageUrl,0, -$count);
return $sPagePath;
}
function output($aErrors = array(), $aResults = array()){ // Output JSON
$bFormSent = empty($aErrors) ? true : false;
$aCombinedData = array(
'bFormSent' => $bFormSent,
'aErrors' => $aErrors,
'aResults' => $aResults
);
header('Content-type: application/json');
echo json_encode($aCombinedData);
exit;
}
// Check supported version of PHP
if (version_compare(PHP_VERSION, '5.2.0', '<')) { // PHP 5.2 is required for the safety filters used in this script
$aErrors[] = 'Unsupported PHP version. <br /><em>Minimum requirement is 5.2.<br />Your version is '. PHP_VERSION .'.</em>';
output($aErrors);
}
if (!empty($_POST)) { // Form posted?
// Get a safe-sanitized version of the posted data
$sFromEmail = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
$sFromName = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
$sMessage = "Name: ".stripslashes_if_required($_POST['name']);
$sMessage .= "\r\nEmail: ".stripslashes_if_required($_POST['email']);
$sMessage .= "\r\nBusiness: ".stripslashes_if_required($_POST['business']);
$sMessage .= "\r\nAddress: ".stripslashes_if_required($_POST['address']);
$sMessage .= "\r\nPhone: ".stripslashes_if_required($_POST['phone']);
$sMessage .= "\r\nMessage: ".stripslashes_if_required($_POST['message']);
$sMessage .= "\r\n--\r\nEmail sent from ". get_current_url_path();
$sHeaders = "From: '$sFromName' <$sFromEmail>"."\r\n";
$sHeaders .= "Reply-To: '$sFromName' <$sFromEmail>";
if (filter_var($sFromEmail, FILTER_VALIDATE_EMAIL)) { // Valid email format?
$bMailSent = mail(TO_EMAIL, "New inquiry from $sFromName", $sMessage, $sHeaders);
if ($bMailSent) {
$aResults[] = "Message sent, thank you!";
} else {
$aErrors[] = "Message not sent, please try again later.";
}
} else {
$aErrors[] = 'Invalid email address.';
}
} else { // Nothing posted
$aErrors[] = 'Empty data submited.';
}
output($aErrors, $aResults);
您是否研究過PHP的preg_replace函數? 您可以使用preg_replace('/[^0-9]/', '', $_POST['phone'])
任何非數字字符。
過濾掉字符數據后,您始終可以檢查它是否具有所需的長度:
$phone = preg_replace('/[^0-9]/', '', $_POST['phone']);
if(strlen($phone) === 10) {
//Phone is 10 characters in length (###) ###-####
}
您還可以使用PHP的preg_match函數,如在另一個SO問題中所討論的。
有幾種方法可以做到...例如:
// If you want to clean the variable so that only + - . and 0-9 can be in it you can:
$number = filter_var($number, FILTER_SANITIZE_NUMBER_INT);
// If you want to clean it up manually you can:
$phone = preg_replace('/[^0-9+-]/', '', $_POST['phone']);
// If you want to check the length of the phone number and that it's valid you can:
if(strlen($_POST['phone']) === 10) {
if (!preg_match('/^[0-9-+]$/',$var)) { // error } else { // good }
}
顯然,可能需要根據國家/地區和其他雜項因素進行一些編輯。
您可以嘗試使用preg_replace過濾掉所有非數字字符,然后可以檢查剩余內容的長度以查看其電話號碼(應為7.9或10位數字)
// remove anything thats not a number from the string
function only_numbers($number) { return preg_replace('/[^0-9]/', '', $number) };
// test that the string is only 9 numbers long
function isPhone($number) { return strlen(only_numbers($number)) == 9; }
驗證后使用該值時,只需確保使用only_numbers
值即可。
-肯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.