PHP strnatcmp() 函数

PHP 教程 · 2019-01-30 09:43:19

实例

使用"自然"算法来比较两个字符串(区分大小写):

<?php
echo strnatcmp("2Hello world!","10Hello world!");
echo "<br>";
echo strnatcmp("10Hello world!","2Hello world!");
?>

定义和用法

strnatcmp() 函数使用一种"自然"算法来比较两个字符串(区分大小写)。

在自然算法中,数字 2 小于数字 10。在计算机排序中,10 小于 2,这是因为 10 中的第一个数字小于 2。

注释:该函数是区分大小写的。

语法

strnatcmp(string1,string2)


参数 描述
string1 必需。规定要比较的第一个字符串。
string2 必需。规定要比较的第二个字符串。

技术细节

返回值: 该函数返回:
  • 0 - 如果两个字符串相等
  • <0 - 如果 string1 小于 string2
  • >0 - 如果 string1 大于 string2
PHP 版本: 4+

更多实例

实例 1

自然算法(strnatcmp)和常规计算机字符串排序算法(strcmp)的不同:

<?php
$arr1 = $arr2 = array("pic1","pic2","pic10","pic01","pic100","pic20","pic30","pic200");
echo "Standard string comparison"."<br>";
usort($arr1,"strcmp");

print_r($arr1);
echo "<br>";

echo "Natural order string comparison"."<br>";
usort($arr2,"strnatcmp");

print_r($arr2);
?>

点击查看所有 PHP 教程 文章: https://www.codercto.com/courses/l/5.html

查看所有标签

Introduction to Linear Optimization

Introduction to Linear Optimization

Dimitris Bertsimas、John N. Tsitsiklis / Athena Scientific / 1997-02-01 / USD 89.00

"The true merit of this book, however, lies in its pedagogical qualities which are so impressive..." "Throughout the book, the authors make serious efforts to give geometric and intuitive explanations......一起来看看 《Introduction to Linear Optimization》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具