Home >Backend Development >PHP Tutorial >PHP function substr_compare() that compares two strings from a specified starting position

PHP function substr_compare() that compares two strings from a specified starting position

黄舟
黄舟Original
2017-11-06 13:25:491458browse

Example

Compare two Strings:

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

Definition and usage

substr_compare() function compares two strings from the specified starting position string.

Tip: This function is binary safe and optionally case sensitive.

Syntax

substr_compare(string1,string2,startpos,length,case)
Parameters Description
string1 Required. Specifies the first string to compare.
string2 Required. Specifies the second string to be compared.
startpos Required. Specifies where in string1 to begin comparison. If negative, counting starts from the end of the string.
length Optional. Specifies the number of characters in string1 to be compared.
case Optional. A Boolean value that specifies whether to perform case-sensitive comparisons:
  • FALSE - Default. Case Sensitive

  • TRUE - Case Insensitive

Technical Details

As of PHP 5.1, negative startpos is allowed.

更多实例

实例 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
?>


Return value: This function returns:
  • 0 - If the two strings are equal

  • 2070e17ca82f186d8feb2bd708b4b26b0 - if string1 (startpos) is greater than string2

If length is greater than or equal to the length of string1, this function returns FALSE.
PHP Version: 5+
##Update Log :

The above is the detailed content of PHP function substr_compare() that compares two strings from a specified starting position. For more information, please follow other related articles on the PHP Chinese website!

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