strnatcasecmp 


string apg

COMPARES two STRINGS using a natural order algorithm.





If $str1 is less than $str2 this function retuns an integer less than ZERO.

If $str1 is equal to $str2 this function returns ZERO.

If $str1 is greater than $str2 this function returns an an integer greater than ZERO.


This function implements a comparison algorithm that orders alphanumeric STRINGS in the way a human being would, this is described as a natural ordering.


This functiom is binary-safe

This function is CASE INSENSITIVE


Natural Order String Comparison  





<?php

int strnatcasecmp 
str $str1 str $str2 )


where,

$str1 The first STRING to compare 

$str2 
The second STRING to compare

?>
 

$str1


The first STRING to be compared.



$str2


The second STRING to be compared.



  1 EXERCISE   

<?php

$strnccmp01a 
"Better late than never!";

$strnccmp01b "BETTER LATE THAN NEVER!";

$lenght01 6
// 6 initial characters

$intnccmp01ab strnatcasecmp ($strnccmp01a
                                                    
$strnccmp01b);

if (
$intnccmp01ab 0)
{
echo 
substr($strnccmp01a0$lenght01)  . 
'<br>is GREATER than<br>' 
substr($strnccmp01b0$lenght01);
}
elseif (
$intnccmp01ab 0)
{
echo 
substr($strnccmp01a0$lenght01) . 
'<br>is LESS than<br>' 
substr($strnccmp01b0$lenght01);
}
else
{
echo 
substr($strnccmp01a0$lenght01) . 
'<br>is EQUAL to<br>' 
substr($strnccmp01b0$lenght01);
}

?> 

  2 EXERCISE   

<?php  

$tst02 
glob('' "*{.JPG,.jpg,.txt,.TXT,
                             .mp3,.MP3,
                             .wav,.WAV,
                             .m4a,.M4A}"
GLOB_BRACE); 

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   
   This code retrieves all files in the  
   specified directory, if they are: 
   .JPG,.jpg,.txt,.TXT,.mp3, .MP3, .wav, .WAV, .m4a, .M4A  
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 

usort($tst02"strnatcasecmp");

foreach(
$tst02 as $ts02)
{
echo 
basename($ts02) . '<br>';
}    

?> 

  3 EXERCISE   

<?php

function str_dump($one$two) {
    echo 
"<br>$one<br>and<br>$two<br><br>Returned value: ";
    
var_dump(strnatcasecmp($one$two));
    echo 
'<br>- - - - - - -<br>';
}

echo 
"Testing strnatcasecmp() : basic functionality.<br>";

// Calling strnatcasecmp() with all possible arguments
echo '- - - - - - -<br>';
str_dump('A''a');
str_dump('a10''A20');
str_dump('A1b''a');
str_dump('x2-y7''x8-y8');
str_dump('1.010''1.001');
str_dump(' ab'' aB');
str_dump('acc ''acc');
str_dump(11.510.5);
str_dump(10.510.5E1);
str_dump('Rfc822.txt''rfc2086.txt');
str_dump('Rfc822.txt''rfc822.TXT');
str_dump('pIc 6''pic   7');
str_dump(0xFFF0Xfff);

?>

  4 EXERCISE   

<?php

/* Preparation */
class a
{
    function 
__toString()
    {
        return 
"Hello WORLD";
    }
}

class 
b
{
    function 
__toString()
    {
        return 
"HELLO world";
    }
}

$a = new a();
$b = new b();

function 
str_dump($a$b) {
    echo 
"<br>$a<br>and<br>$b<br>";
    
var_dump(strnatcasecmp($a$b));
    echo 
'<br><br>';
}

echo 
"Testing strnatcasecmp() : variation:<br>";

str_dump('0'false);
str_dump('fooBar''');
str_dump('', -1);
str_dump("Hello\0world""Helloworld");
str_dump("\x0""\0");
str_dump($a$b);

?>