PHP strncasecmp字符串比较的小技巧
作者:bea
只是这样就需要转换两次。大多时候,我们是针对字符集转换的时候才会这样,比如判断参数传进来是否utf-8,这5个字符的写法,可就多了,比如UTF-8,Utf-8,utf-8等,那我们怎么办呢?strtolower?strupper?不需要啦。。 strncasecmp($a,$b,$length)就可以了。。 如果返回是0则相等,那我们怎么判断呢? strncasecmp($str,'utf-8',5) == 0那么,传入的参数就是utf8的,是否很方便呢? 只是这些函数我们平
只是这样就需要转换两次。大多时候,我们是针对字符集转换的时候才会这样,比如判断参数传进来是否utf-8,这5个字符的写法,可就多了,比如UTF-8,Utf-8,utf-8等,那我们怎么办呢?strtolower?strupper?不需要啦。。
strncasecmp($a,$b,$length)就可以了。。
如果返回是0则相等,那我们怎么判断呢?
strncasecmp($str,'utf-8',5) == 0那么,传入的参数就是utf8的,是否很方便呢?
只是这些函数我们平时不太用得到,我看到这个函数的用法却是在 yii framework,他在处理事件的时候,判断前两个字符是否为 on 的时候,就是这样判断的。我也因此学到了一招。
strncasecmp Definition and Usage
定义和用法
The strncasecmp() function compares two strings.
strncasecmp()函数的作用是:比较字符串的前n个字符(大小写不敏感)。
This function returns:
这个函数将返回下列值:
0 - if the two strings are equal
0 – 如果字符串相等
<0 - if string1 is less than string2
<0 – 如果string1小于string2
>0 - if string1 is greater than string2
>0 – 如果string1大于string2
Syntax
语法
strncasecmp(string1,string2,length)
Parameter参数
Description描述
string1
Required. Specifies the first string to compare必要参数。指定参与比较的第一个字符串对象
string2
Required. Specifies the second string to compare必要参数。指定参与比较的第二个字符串对象
length
Required. Specify the number of characters from each string to be used in the comparison必要参数。指定每个字符串中参数比较的字符数量
Tips and Notes
注意点
Note: The strncasecmp() is binary safe and case-insensitive.
注意:strncasecmp()函数是二进制精确的,并且它不区分字母大小写。
Example
案例
代码如下:
<?php
echo strncasecmp("Hello world!","hello earth!",6);
?>
The output of the code above will be:
上述代码将输出下面的结果:
0
有用 | 无用
strncasecmp($a,$b,$length)就可以了。。
如果返回是0则相等,那我们怎么判断呢?
strncasecmp($str,'utf-8',5) == 0那么,传入的参数就是utf8的,是否很方便呢?
只是这些函数我们平时不太用得到,我看到这个函数的用法却是在 yii framework,他在处理事件的时候,判断前两个字符是否为 on 的时候,就是这样判断的。我也因此学到了一招。
strncasecmp Definition and Usage
定义和用法
The strncasecmp() function compares two strings.
strncasecmp()函数的作用是:比较字符串的前n个字符(大小写不敏感)。
This function returns:
这个函数将返回下列值:
0 - if the two strings are equal
0 – 如果字符串相等
<0 - if string1 is less than string2
<0 – 如果string1小于string2
>0 - if string1 is greater than string2
>0 – 如果string1大于string2
Syntax
语法
strncasecmp(string1,string2,length)
Parameter参数
Description描述
string1
Required. Specifies the first string to compare必要参数。指定参与比较的第一个字符串对象
string2
Required. Specifies the second string to compare必要参数。指定参与比较的第二个字符串对象
length
Required. Specify the number of characters from each string to be used in the comparison必要参数。指定每个字符串中参数比较的字符数量
Tips and Notes
注意点
Note: The strncasecmp() is binary safe and case-insensitive.
注意:strncasecmp()函数是二进制精确的,并且它不区分字母大小写。
Example
案例
代码如下:
<?php
echo strncasecmp("Hello world!","hello earth!",6);
?>
The output of the code above will be:
上述代码将输出下面的结果:
0
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- PHP文件读写操作之文件读取方法详解
- PHP目录函数实现创建、读取目录教程实例
- PHP开发规范手册之PHP代码规范详解
- PHP JSON格式数据交互实例代码详解
- PHP学习笔记之二 php入门知识
- php算开始时间到过期时间的相隔的天数
- php数据库密码的找回的步骤
- 重新封装zend_soap实现http连接安全认证的php代码
- php 变量未定义等错误的解决方法
- 兼容性比较好的PHP生成缩略图的代码
- php的日期处理函数及uchome的function_coomon中日期处理函数的研究
- PHP日期处理函数 整型日期格式
- Base64在线编码解码实现代码 演示与下载
- php !function_exists("T7FC56270E7A70FA81A5935B72EACBE29"))代码解密
- PHP备份/还原MySQL数据库的代码
- php循环检测目录是否存在并创建(循环创建目录)
- 全局记录程序片段的运行时间 正确找到程序逻辑耗时多的断点
- Discuz Uchome ajaxpost小技巧
- php INI配置文件的解析实现分析