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

黄舟
Release: 2023-03-17 07:10:01
Original
1380 people have browsed it

Example

Compare two Strings:

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

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)
Copy after login
ParametersDescription
string1 Required. Specifies the first string to compare.
string2Required. Specifies the second string to be compared.
startposRequired. 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.
caseOptional. 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);
?>
Copy after login

实例 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);
?>
Copy after login

实例 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
?>
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Return value: This function returns:
  • 0 - If the two strings are equal

  • <0 - if string1 (startpos) is less than string2

  • >0 - 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 :