我想找到最轻量级的解决方案来验证字符串为a letter or number
+ ?
。例如:a?
或1?
等PHP验证字符串轻量级
0
A
回答
4
1
略低快于正则表达式的功能:
// return true or false
function validate($str) {
$str0 = ord($str[0]);
return(
(
($str0 >= 97 && $str0 <= 122) or
($str0 >= 48 && $str0 <= 57)
) &&
(
$str[1] == '?'
)
);
}
-1
确定这是最快的方式
$allowed_char = Array();
for($i=ord('a');$i<=ord('z');$i++) $allowed_char[chr($i)] = true;
for($i=ord('0');$i<=ord('9');$i++) $allowed_char[chr($i)] = true;
function validate($str) {
global $allowed_char;
return $allowed_char[$str[0]] && $str[1] == '?' && !isset($str[2]);
}
正则表达式= 2.0147299766541 s
该解决方案= 1.6041090488434s
所以它比正则表达式的解决方案:)
+1
包括数组的构造?为什么不制作一个完整的静态查找表?顺便说一下,“轻量级”并不一定意味着“最快”。 ; O) – deceze
0
快20%,前一段时间,我写了一个轻量级的验证类。也许你可以使用它。
例如:
$oValidator = new Validator();
$oValidator->isValid('a', 'alpha_numeric|max_length[1]'); //true
$oValidator->isValid('1', 'alpha_numeric|max_length[1]'); //true
$oValidator->isValid('ab', 'alpha_numeric|max_length[1]'); //false
$oValidator->isValid('1337', 'alpha_numeric|max_length[1]'); //false
例子: http://sklueh.de/2012/09/lightweight-validator-in-php/
github上: https://github.com/sklueh/Lightweight-PHP-Validator
相关问题
- 1. 轻量级文件验证与PKI
- 2. PHP验证和验证字符串
- 3. PHP字符串验证
- 4. 轻量级PHP CRUD
- 5. 希腊字母验证PHP字符串
- 6. 验证字符串
- 7. 验证字符串
- 8. 轻量级PHP REST API
- 9. PHP中的轻量级CMS
- 10. 从URL验证PHP中的字符串
- 11. PHP使用preg_match验证字符串
- 12. 使用php进行字符串验证
- 13. 验证查询字符串在PHP
- 14. 在PHP中验证LESS字符串
- 15. php查询字符串验证
- 16. PHP查找字符串并验证它
- 17. 验证变量包含字符串值
- 18. .Net对象的轻量级字符串序列化
- 19. 字符串字母验证
- 20. 如何使用jquery验证php md5字符串验证
- 21. 只验证字符串php中的选定字符(电话号码验证)
- 22. 轻量级重量级
- 23. 验证轻量级核心数据迁移
- 24. 使用MV-VM的WPF轻量级验证框架
- 25. 具有身份验证支持的轻量级转发代理
- 26. 轻量级像
- 27. 轻量级JRE
- 28. 验证Facebook DateTime字符串
- 29. 字符串索引验证
- 30. 验证日期字符串
真的有重液检查仅2个字符? –
你可以看看正则表达式。这里有一些关于[如何工作]的例子(http://php.net/manual/en/function.preg-match.php) – Ibu
is_string()....? – sdolgy