PHP substr_compare() 函数

PHP 教程 · 2019-01-30 13:44:32

实例

比较两个字符串:

<?php
echo substr_compare("Hello world","Hello world",0);
?>

定义和用法

substr_compare() 函数从指定的开始位置比较两个字符串。

提示:该函数是二进制安全和选择性区分大小写。

语法

substr_compare(string1,string2,startpos,length,case)

参数 描述
string1 必需。规定要比较的第一个字符串。
string2 必需。规定要比较的第二个字符串。
startpos 必需。规定在 string1 中的何处开始比较。如果为负数,则从字符串末端开始计数。
length 可选。规定在 string1 中参与比较的字符数。
case 可选。一个布尔值,规定是否执行区分大小写的比较:
  • FALSE - 默认。区分大小写
  • TRUE - 不区分大小写

技术细节

返回值: 该函数返回:
  • 0 - 如果两字符串相等
  • <0 - 如果 string1 (从开始位置 startpos)小于 string2
  • >0 - 如果 string1 (从开始位置 startpos)大于 string2
如果 length 大于或等于 string1 的长度,则该函数返回 FALSE。
PHP 版本: 5+
更新日志: 自 PHP 5.1 起,允许使用负数的 startpos

更多实例

实例 1

比较两个字符串,当 string1 中用于比较的开始位置为 6 时:

<?php
echo substr_compare("Hello world","world",6);
?>

实例 2

使用所有的参数:

<?php
echo substr_compare("world","or",1,2);
echo substr_compare("world","ld",-2,2);
echo substr_compare("world","orl",1,2);
echo substr_compare("world","OR",1,2,TRUE);
echo substr_compare("world","or",1,3);
echo substr_compare("world","rl",1,2);
?>

实例 3

不同的返回值:

<?php
echo substr_compare("Hello world!","Hello world!",0); // the two strings are equal
echo substr_compare("Hello world!","Hello",0); // string1 is greater than string2
echo substr_compare("Hello world!","Hello world! Hello!",0); // str1 is less than str2
?>

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

查看所有标签

The Web Designer's Idea Book

The Web Designer's Idea Book

Patrick Mcneil / How / 2008-10-6 / USD 25.00

The Web Designer's Idea Book includes more than 700 websites arranged thematically, so you can find inspiration for layout, color, style and more. Author Patrick McNeil has cataloged more than 5,000 s......一起来看看 《The Web Designer's Idea Book》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具