Home  >  Article  >  php教程  >  php常用字符串比较函数实例汇总

php常用字符串比较函数实例汇总

WBOY
WBOYOriginal
2016-06-06 20:16:521035browse

这篇文章主要介绍了php常用字符串比较函数,实例汇总了substr_compare、strncasecmp、strncmp、strcoll等常用函数,具有一定的参考借鉴价值,需要的朋友可以参考下

本文实例汇总了php常用字符串比较函数。分享给大家供大家参考。具体分析如下:

substr_compare() 函数从指定的开始长度比较两个字符串,该函数返回:

0 - 如果两字符串相等,0 - 如果 string1 (从开始位置)大于 string2.

语法:substr_compare(string1,string2,startpos,length,case),代码如下:

复制代码 代码如下:

$str1="hello world";          //定义字符串1
$str2="hello world";          //定义字符串2
$result=substr_compare($str1,$str2,1,10);      //执行比较操作
echo $result;           //输出结果,1


strnatcasecmp() 函数使用一种"自然"算法来比较两个字符串,在自然算法中,数字 "2" 小于数字 "10",在计算机排序中,"2" 大于 "10",这是因为 "2" 大于 "10" 的第一个数字,代码如下:

复制代码 代码如下:

$str1="hello world";          //定义字符串1
$str2="hello world";          //定义字符串2
$result=strnatcasecmp($str1,$str2);       //执行比较操作
echo $result;           //输出结果,0


strncasecmp() 函数比较两个字符串,该函数返回:

0 - 如果两个字符串相等,0 - 如果 string1 大于 string2.

语法:strncasecmp(string1,string2,length),代码如下:

复制代码 代码如下:

$str1="hello world";          //定义字符串1
$str2="hello world";          //定义字符串2
$result=strncasemp($str1,$str2,7);       //执行比较操作
echo $result;           //输出结果,,0


strncmp() 函数比较两个字符串,该函数返回:

0 - 如果两个字符串相等,0 - 如果 string1 大于 string2.

语法:strncmp(string1,string2,length),代码如下:

复制代码 代码如下:

$str1="hello world";          //定义字符串1
$str2="hello world";          //定义字符串2
$result=strncmp($str1,$str2,7);        //执行比较操作
echo $result;           //输出结果,1


strcoll() 函数比较两个字符串,该函数返回:

0 - 如果两个字符串相等,0 - 如果 string1 大于 string2.

字符串的比较会根据本地设置而变化,aa.

语法:strcoll(string1,string2),代码如下:

复制代码 代码如下:

$str1="hello world";          //定义字符串1
$str2="hello world";          //定义字符串2
$result=strcoll($str1,$str2);        //执行比较操作
echo $result;           //输出结果,1

希望本文所述对大家的PHP程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:浅析php创建者模式Next article:浅析php工厂模式