网络营销电子商务研究中心  
How to buy the best prescription safety glasses in Canada? Let's study!
Go Back   网络营销电子商务研究中心 > 网站建设 > 代码交流
User Name
Password
 
FAQ Members List Calendar Cheap Glasses

代码交流 ASP,PHP,JSP等网站源代码下载与交流。

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1   IP: 222.76.215.18
Old 2008-01-24, 10:18 PM
car car is offline
高级会员
 
Join Date: 2006-05-14
Posts: 534
car 正向着好的方向发展
Default 电话号码 php是否是字母或数字

对于用户输入的任何数据,如从表单提交中获取的数据,即使不考虑恶意企图,程序员也必须做最坏的打算。因此,程序员总是要面对检查数据是否正确及可用的问题。本章会展示一些在日常工作中能够发挥作用的算法与函数。

在检查数据正确性时,分解为两个步骤将会带来很大便利:一是将输入数据标准化,二是检查标准化之后的数据。输入数据的“标准化”就是将得到的数据调整为统一或是标准格式。各种信息本来都会有一些不同的表达方式,但对于特定的应用程序来说,它们都需要被转化为某种标准格式。之后,我们就可以对标准格式的数据应用有效性规则,从而确定输入的数据是可用的。

将数据检验分为两个步骤可以使过程更加简单和灵活。标准化过程只关心格式问题,程序可以让用户以更自由的方式提供信息,从而具有更高的用户友好度。当输入信息被标准化之后,检查过程就简单多了,因为信息格式是已知的。

当然,凡事皆有例外,有些场合就不适合这种标准化之后进行检验的方法。例如,有时表单输入是数据检验的组成部分,这样就打破了两个步骤的次序;如果输入信息不具有期望的格式,它就会立即变得无效。

下面这些范例都假设用户被要求以某种格式提供信息,而且没有其他程序对用户输入进行处理,范例中得到的数据是“原始的”。

快速提示
Ø 检查变量是否是数字或数字式字符串:

$success = is_numeric($variable);

如果变量是数字,或者是包含数字及符号、小数点、指数的字符串,这个函数就会返回True。

完整文档:http://php.net/is_numeric。

Ø 检查字符串是否只包含字母数字:

$success = ctype_alnum($string);

如果字符串里只包含字母或数字,这个函数就会返回True。

完整文档:http://php.net/ctype_alnum。

Ø 检查字符串是否只包含字母:

$success = ctype_alpha($string);

如果字符串里只包含字母“a”到“z”(不分大小写),函数就会返回True。

完整文档:http://php.net/ctype_alpha。

Ø 检查字符串里是否只包含数字:

$success = ctype_digit($string);

如果字符串里只包含字符“0”到“9”,函数就会返回True。注意,不允许有小数点。

完整文档:http://php.net/ctype_digit。

Ø 检查字符串里是否只包含十六进制数字:

$success = ctype_xdigit($string);

允许的字符是“0”到“9”和“a”到“f”(无论大小写)。

完整文档:http://php.net/ctype_xdigit。

Ø 检查字符串是否全是大写的或全是小写的:

$success = ctype_upper($string);

$success = ctype_lower($string);

如果字符串包含不同的大小写,函数就会返回False。

完整文档:http://php.net/ctype_upper和http://php.net/ctype_lower。

Ø 强制变量为指定类型:

$success = settype($variable, $type);

PHP利用其内部类型转化将变量强制为指定类型。如果转化不能实现,函数会返回False。

完整文档:http://php.net/settype。

Ø 使用类型转化将变量解释为特定类型:

$result = (typekeyword) $variable;

PHP将变量值从原始类型转化为typekeyword指定的类型。

完整文档:http://php.net/manual/language.types.type-juggling.php。

11.1 电话号码
本例中处理的电话号码是美国使用的标准格式,它包含三位区号,随后是三位局号,最后是四位数字。不幸的是,显示(及输入)这些数字的格式有很多种,如(999)555-0100、(999) 555-0100、9995550100、999.555.0100和999-555-0100。程序清单11.1.1可以接收上述(及其他)格式的数字,并且把它们标准化为最后一种形式,因为这便于计算机进行后续处理。

在检验电话号码时,除了确定它包含适当数量的数字之外,还有其他一些规则。例如,如果局号是555,那么后四位数字就不能在0100~0199之间,因为它们是被保留的。另外,区号不能以0或1开头,并且第二个数字不能为9。根据这些规则,我们就得到了如程序清单11.1.1所示的函数。

程序清单11.1.1 电话号码函数库

<?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', '354-555-0103', '294.423.8437',

'301 867-5309', '424-726 739', '829-56628426');

foreach ($phones as $num) {

$st = standardize_phone($num);

$valid = validate_phone($st);

$output = $valid ? 'Valid' : 'Invalid';

echo "<p>{$st} - {$output}</p>\n";

}

?>

上述这些函数不允许电话号码以1开头,而有时人们确实会这样输入电话号码(因为美国的国家区号就是1)。如果想允许用户这样输入,只需要很简单的修改。最简单的方式是在标准化程序中删除号码前面的1就可以了,因为国内区号是不能以1开始的。
Reply With Quote
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off

Forum Jump

Prescription-glasses.com offers prescription glasses online at discount prices.
All times are GMT +8. The time now is 12:26 AM.


Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.