下载此文档

数据检验与标准化.pdf


文档分类:行业资料 | 页数:约17页 举报非法文档有奖
1/17
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/17 下载此文档
文档列表 文档介绍
: .

,显示(及输入)这些数字的格式有很多种,
如(999)555-0100、(999) 555-0100、9995550100、 和 999-555-0100。程序清单 可以接收上述(及其他)格式的数字,并且把它们标准化
为最后一种形式,因为这便于计算机进行后续处理。
在检验电话号码时,除了确定它包含适当数量的数字之外,还有其他一些规
则。例如,如果局号是 555,那么后四位数字就不能在 0100~0199 之间,因为
它们是被保留的。另外,区号不能以 0 或 1 开头,并且第二个数字不能为 9。根
据这些规则,我们就得到了如程序清单 所示的函数。
程序清单 电话号码函数库
<?php
// A function that will accept US phone numbers, in most every format
// and standardize them to xxx-xxx-xxxx format.
function standardize_phone($phone) {
// First, remove all non-digits from the string
$p = preg_replace('/[^0-9]/', '', $phone);
// Now, break it into its appropriate parts and insert dashes.
return substr($p, 0, 3) . '-' . substr($p, 3, 3) . '-' . substr($p, 6);
}
// A function to check for phone number validity
// It requires a standardized number
function validate_phone($phone) {
// First split the number into 3 parts:
$parts = explode('-', $phone);
// If the middle is '555'
if ($parts[1] == '555') {
// Invalid if the final part is between 0100 and 0199
if (($parts[2] >= 100) && ($parts[2] < 200)) {
return false;
}}
// Invalid if the first digit of the area code is 0 or 1
if ($parts[0] < 200) {
return false;
}
// Invalid if the second digit of the area code, is 9
if ($parts[0]{1} == '9') {
return false;
}
// Check that the last number has 4 characters:
if (strlen($parts[2]) != 4) {
return false;
}
// Otherwise, we made it, it's valid.
return true;
}
// Standardize & validate some phone numbers:
$phones = array('(108)355-4688

数据检验与标准化 来自淘豆网www.taodocs.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数17
  • 收藏数0 收藏
  • 顶次数0
  • 上传人kunpengchaoyue
  • 文件大小753 KB
  • 时间2022-05-20