PHP字符串比较函数strcmp()和strcasecmp()使用总结,strcmpstrcasecmp
PHP字符串比较函数strcmp()和strcasecmp()使用总结,strcmpstrcasecmp
比较字符串是任何编程语言的字符串处理功能中重要的特性之一。在PHP中除了可以使用比较运算符号(==、)加以比较外,还提供了一系列的比较函数,使PHP可以进行更复杂的字符串比较。如strcmp()、strcasecmp()和strnatcmp()等函数。
1.按字节顺序进行字符串比较
要按字节顺序进行字符串的比较,可以使用strcmp()和strcasecmp()两个函数,其中函数strcasecmp()可以忽略字符串中字母的大小写进行比较。这两个函数的原型如下所示:
复制代码 代码如下:
in strcmp(string str1,string str2) //区分字符串中字母大小写地比较
int strcasecmp(string str1,string str2) //忽略字符串中字母大小写地比较
这两个函数的用法相似,都需要传入进行比较的两个字符串参数。可以对输入的str1和str2两字符串,按照字节的ASCII值从两个字符串的首字节开始比较,如果相等则进入下一个字节的比较,直至结束比较。返回以下三个值之一:
★如果str1等于str2则返回0。
★如果str1大于str2则返回1。
★如果str1小于str2则返回-1。
在下面的程序中通过比较后的返回值判断两个比较字符串大小。使用strcmp()函数区分字符串中字母大小写的比较,使用strcasecmp()函数忽略字符串中字母大小写的比较。当然没有实际意义。代码如下所示:
复制代码 代码如下:
$username = "Admin";
$password = "lampBrother";
//不区分大小写的比较,如果两个字符串相等返回0
if(strcasecmp($userName,"admin")== 0){
echo "用户名存在";
}
//将两个比较的字符串相应的函数转成全大写或全小写后,也可以实现不区分大小写的比较
if(strcasecmp(strtolower($userName),strtolower("admin")) == 0){
echo "用户名存在";
}
//区分字符串中字母的大小写比较
switch(strcmp($password,"lampbrother")){
case 0:
echo "两个字符串相等
"; break;
case 1:
echo "第一个字符串大于第二个字符串
"; break;
case -1:
echo "第一个字符串小于第二个字符串
"; break;
}
?>
2.按自然排序进行字符串比较
除了可以按照字节位的字典顺序进行比较外,PHP还提供了按照“自然排序”法对字符串进行比较。所谓自然排序,是指按照人们的日常生活中的思维习惯进行排序,即将字符串中的数字部分按照数字大小进行比较。例如按照字节比较时“4”大于“33”,因为“4”大于“33”中的第一个字符,而按照自然排序法则“33”大于“4”。使用strnatcmp()函数按自然排序法比较两个字符串,该函数对大小写敏感,其使用格式与strcmp()函数相似。
在下面的例子中,对一个数组中带有数字的文件名,使用冒泡排序法通过两种比较方法排序。代码如下所示:
复制代码 代码如下:
//定义一个包含数字值的数组
$files = array("file11.txt","file22.txt","file1.txt","file2.txt");
function mySort($arr,$select = false){
for($i=0;$i
if($select){
//前后两个值比较结果大于0则交换位置
if(strcmp($arr[$j],$arr[j+1])>0){
$tmp = $arr[$j];
$arr[$j] = $arr[$j+1];
$arr[$j+1] = $tmp;
}
//如果第二个参数为false则使用strnatcmp()函数比较大小
}else{
//如果比较结果大于0交换位置
if(strnatcmp($arr[$j],$arr[$j+1])>0){
$tmp = $arr[$j];
$arr[$j] = $arr[$j+1];
$arr[$j+1]; = $tmp;
}
}
}
}
return $arr; //排序后的数组
}
print_r(mySort($files,true)); //选择按字典顺序排序: file1.txt file11.txt file2.txt file22.txt
print_r(mySort($files,false)); //选择按自然顺序排序:file1.txt file2.txt file11.txt file22.txt
?>
在PHP中也提供了这个函数忽略大小写的版本的函数strnatcasecmp()用法与strnatcmp()函数相同。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
