Commonly used string comparison functions in php

高洛峰
Release: 2016-11-29 13:33:54
Original
1100 people have browsed it

The

substr_compare() function compares two strings from the specified starting length. The function returns:

0 - if the two strings are equal, <0 - if string1 (from the starting position) is less than string2, >0 - if string1 (from the starting position) is greater than string2.

Syntax: substr_compare(string1,string2,startpos,length,case), the code is as follows:

$str1="hello world"; //Define string 1

$str2=" hello world"; //Define string 2

$result=substr_compare($str1,$str2,1,10); //Perform comparison operation

echo $result; The function compares two strings using a "natural" algorithm. In the natural algorithm, the number "2" is less than the number "10". In computer sorting, "2" is greater than "10". This is because "2" is greater than The first number of "10", the code is as follows:

$str1="hello world";                                                                                                                                       using using using using using using          out out through off ’ ’s ’ ’s ’ through  through using off ‐ ‐ ‐out through‐ ‐window to be the first digit of "10".                                              ($str1,$str2); //Perform comparison operation

echo $result; Equal, <0 - if string1 is less than string2, >0 - if string1 is greater than string2.

Syntax: strncasecmp(string1,string2,length), the code is as follows:

$str1="hello world"; //Define characters String 1

$str2="hello world"; //Define string 2

$result=strncasemp($str1,$str2,7); //Perform comparison operation

echo $result; //Output the result, 0

strncmp() function compares two strings. The function returns:

0 - If the two strings are equal, <0 - If string1 is less than string2, >0 - If string1 is greater than string2.

Syntax: strncmp(string1,string2,length), the code is as follows:

$str1="hello world";                                                                                                           strncmp (string1, string 2, length), the code is as follows:

$str1="hello world"; ($str1,$str2,7);                           //Perform comparison operation

echo   $result;                                                                                                   ’ ’ ’ ’ ’ ’ s ’s ’ s ’ s ’ s to ‐ ‐ ‐ ‐ ​ ​ ​                               to to String equality, <0 - if string1 is less than string2, >0 - if string1 is greater than string2.

String comparison will change according to local settings, aa.

Syntax: strcoll(string1 , string2), the code is as follows:

$str1="hello world"; //Define string 1

$str2="hello world"; //Define string 2

$result=strcoll($str1,$ str2);              //Perform comparison operation

echo $result;

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!