0% found this document useful (0 votes)
102 views5 pages

Matching

The document describes several PHP functions for comparing strings: - strcmp performs a binary-safe case-sensitive comparison and returns <0 if str1<str2, >0 if str1>str2, and 0 if equal. - strcasecmp performs a case-insensitive comparison. - strncmp compares a maximum of n characters in a case-sensitive way. - strncasecmp compares a maximum of n characters in a case-insensitive way. - strnatcmp compares strings in a "natural order" that a human would use, prioritizing number meaning over alphabetical order. - strnatcasecmp performs a natural order comparison in a case-
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views5 pages

Matching

The document describes several PHP functions for comparing strings: - strcmp performs a binary-safe case-sensitive comparison and returns <0 if str1<str2, >0 if str1>str2, and 0 if equal. - strcasecmp performs a case-insensitive comparison. - strncmp compares a maximum of n characters in a case-sensitive way. - strncasecmp compares a maximum of n characters in a case-insensitive way. - strnatcmp compares strings in a "natural order" that a human would use, prioritizing number meaning over alphabetical order. - strnatcasecmp performs a natural order comparison in a case-
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

strcmp

strcmp Binary safe string comparison

int strcmp ( string $str1, string $str2 )

Note that this comparison is case sensitive.

Parameters

str1

The first string.

str2

The second string.

Return Values

Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are
equal.

See Also

preg_match()
strcasecmp()
substr()
stristr()
strncasecmp()
strncmp()
strstr()

strcasecmp

strcasecmp Binary safe case-insensitive string comparison

int strcasecmp ( string $str1, string $str2 )

Binary safe case-insensitive string comparison.

Return Values
Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are
equal.

Examples

Example 2463. strcasecmp() example

<?php
$var1 = "Hello";
$var2 = "hello";
if (strcasecmp($var1, $var2) == 0) {
echo '$var1 is equal to $var2 in a case-insensitive string compariso
n';
}
?>

strncmp

strncmp Binary safe string comparison of the first n characters

int strncmp ( string $str1, string $str2, int $len )

This function is similar to strcmp(), with the difference that you can specify the (upper
limit of the) number of characters from each string to be used in the comparison.

Note that this comparison is case sensitive.

str1

The first string.

str2

The second string.

len

Number of characters to use in the comparison.

Return Values

Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are
equal.
strncasecmp

strncasecmp Binary safe case-insensitive string comparison of the first n characters

int strncasecmp ( string $str1, string $str2, int $len )

This function is similar to strcasecmp(), with the difference that you can specify the
(upper limit of the) number of characters from each string to be used in the comparison.

str1

The first string.

str2

The second string.

len

The length of strings to be used in the comparison.

Return Values

Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are
equal.

strnatcmp

strnatcmp String comparisons using a "natural order" algorithm

int strnatcmp ( string $str1, string $str2 )

This function implements a comparison algorithm that orders alphanumeric strings


in the way a human being would, this is described as a "natural ordering". Note
that this comparison is case sensitive.

str1

The first string.

str2
The second string.

Return Values

Similar to other string comparison functions, this one returns < 0 if str1 is less than
str2; > 0 if str1 is greater than str2, and 0 if they are equal.

Examples

An example of the difference between this algorithm and the regular computer string
sorting algorithms (used in strcmp()) can be seen below:

<?php
$arr1 = $arr2 = array("img12.png", "img10.png", "img2.png", "img1.png");
echo "Standard string comparison\n";
usort($arr1, strcmp);
print_r($arr1);
echo "\nNatural order string comparison\n";
usort($arr2, "strnatcmp");
print_r($arr2);
?>

The above example will output:

Standard string comparison


Array
(
[0] => img1.png
[1] => img10.png
[2] => img12.png
[3] => img2.png
)

Natural order string comparison


Array
(
[0] => img1.png
[1] => img2.png
[2] => img10.png
[3] => img12.png
)

strnatcasecmp

strnatcasecmp Case insensitive string comparisons using a "natural order" algorithm

int strnatcasecmp ( string $str1, string $str2 )


This function implements a comparison algorithm that orders alphanumeric strings in the
way a human being would. The behaviour of this function is similar to strnatcmp(),
except that the comparison is not case sensitive. For more information see: Martin Pool's
Natural Order String Comparison page.

str1

The first string.

str2

The second string.

Return Values

Similar to other string comparison functions, this one returns < 0 if str1 is less than str2
> 0 if str1 is greater than str2, and 0 if they are equal.

You might also like