Php strnatcasecmp
From w3cyberlearnings
Contents |
PHP function strnatcasecmp
This function uses the 'natural' algorithm to compare two input strings. String natural algorithm case-insensitive comparision function.
Syntax strnatcasecmp
- string1: string input 1
- string2: string input 2
strnatcasecmp(string1,string2)
Return
- 0: when two string equal.
- >0: string1 is greater than string2.
- <0: string2 is greater than string1.
Natural Order Algorithm
The natural algorithm is to order information in the ways that human readable.
Example
- Unorder list: app1, app3, app10, app20, app12
- Natural Algorithm: app1, app3, app10, app12, app20
Example 1
<?php echo strnatcasecmp('Good','dooG'); ?>
Output: 'Good' is greater than 'dooG'
1
Example 2: strnatcasecmp() is case insensitive
<?php echo strnatcasecmp('GOOD','good'); ?>
Output:Two strings are equal
0
Example 3
<?php $str1 = "good1"; $str2 = "good2"; $str3 = "good10"; if (strnatcasecmp($str1, $str2) > 0) { echo "{$str1} is greater than {$str2}<br/>"; } else if (strnatcasecmp($str1, $str3) > 0) { echo "{$str1} is greater than {$str3}<br/>"; } else { echo "{$str1} is smaller than {$str2} and {$str3}<br/>"; } ?>
Output
good1 is smaller than good2 and good10
Example 4
<?php $str1 = "good09"; $str2 = "good10"; echo strnatcasecmp($str1,$str2); ?>
Output
-1