This functiom is binary-safe
<?php
int strncasecmp ( str $str1 , str $str2 , int $len )
where,
$str1 = The first STRING to compare
$str2 = The second STRING to compare
$len = The length of STRINGS to be used in the comparison
?>
<?php
$strnccmp01 = "Practice makes perfect.";
$strnccmp02 = "PRACTICE MAKES PERFECT.";
$lenght = 8;
// 8 intitial characters
$intnccmp0102 = strncasecmp($strnccmp01,
$strnccmp02,
$lenght);
if ($intnccmp0102 > 0)
{
echo substr($strnccmp01, 0, $lenght) .
'<br>is GREATER than<br>' .
substr($strnccmp02, 0, $lenght);
}
elseif ($intnccmp0102 < 0)
{
echo substr($strnccmp01, 0, $lenght) .
'<br>is LESS than<br>' .
substr($strnccmp02, 0, $lenght);
}
else
{
echo substr($strnccmp01, 0, $lenght) .
'<br>is EQUAL to<br>' .
substr($strnccmp02, 0, $lenght);
}
?>
<?php
$arrdw02 = ['Doc', 'Grumpy',
'Happy', 'Sleepy',
'Bashful', 'Sneezy', 'Dopey'];
$tstwr02 = 'dwarf';
foreach($arrdw02 as $dw02)
{
$ct02 = strncasecmp($tstwr02, $dw02, strlen($dw02));
if($ct02 > 0)
{
$res02 = ' is GREATER than ';
}
elseif ($ct02 < 0)
{
$res02 = ' is LESS than ';
}
else
{
$res02 = ' is EQUAL to ';
}
echo 'Returned value: ' . $ct02 . '<br>' .
$tstwr02 .
$res02 .
$dw02 . '<br><br>';
}
?>
<?php
/*
Run this exercise several times.
With each new run a new result
will be obtained.
*/
$ndw = mt_rand(1, 3);
$nwr = mt_rand(0, 6);
if($ndw == 1)
{
$arr = ['DOC', 'GRUMPY',
'HAPPY', 'SLEEPY',
'BASHFUL', 'SNEEZY', 'DOPEY'];
}
elseif($ndw == 2)
{
$arr = ['doc', 'grumpy',
'happy', 'sleepy',
'bashful', 'sneezy', 'dopey'];
}
else
{
$arr = ['Doc', 'Grumpy',
'Happy', 'Sleepy',
'Bashful', 'Sneezy', 'Dopey'];
}
$str = $arr[$nwr];
foreach($arr as $dw03)
{
$ct03 = strncasecmp($str, $dw03, strlen($dw03));
if($ct03 > 0)
{
$res03 = ' is GREATER than ';
}
elseif ($ct03 < 0)
{
$res03 = ' is LESS than ';
}
else
{
$res03 = ' is EQUAL to ';
}
echo 'Returned value:' . $ct03 . '<br>' .
$str .
$res03 .
$dw03 . '<br><br>';
}
?>
<?php
echo "Testing strncasecmp() function: basic functionality.<br>";
function str_ndump($one, $two, $three) {
echo "<br>$one<br>and<br>$two<br>and<br>$three<br><br>Returned value: ";
var_dump(strncasecmp($one, $two, $three));
echo '<br>- - - - - - -<br>';
}
echo "<br><br>Testing strncasecmp()
with single quoted string:<br>- - - - - - -<br>";
str_ndump('Hello', 'Hello', 5); //expected: int(0)
str_ndump('Hello', 'Hi', 5); //expected: value < 0
str_ndump('Hi', 'Hello', 5); //expected: value > 0
echo "<br><br>Testing strncasecmp()
with double quoted string:<br>- - - - - - -<br>";
str_ndump("Hello", "Hello", 5); //expected: int(0)
str_ndump("Hello", "Hi", 5); //expected: value < 0
str_ndump("Hi", "Hello", 5); //expected: value > 0
echo "<br><br>Testing strncasecmp()
with here-doc string:<br>- - - - - - -<br>";
$str = <<<HEREDOC
Hello
HEREDOC;
str_ndump($str, "Hello", 5); //expected: int(0)
str_ndump($str, "Hi", 5); //expected: value < 0
str_ndump("Hi", $str, 5); //expected: value > 0
?>
<?php
/* Test strncasecmp() function
with upper-case and lower-case
alphabets as inputs for
'str1' and 'str2'
*/
echo "Test strncasecmp() function: with alphabets.<br>";
echo "<br><br>Passing upper-case letters for 'str1':<br>";
for($ASCII = 65; $ASCII <= 90; $ASCII++) {
var_dump( strncasecmp( chr($ASCII), chr($ASCII), 1 ) );
//comparing uppercase letter with
//corresponding uppercase letter; exp: int(0)
echo '<br>';
var_dump( strncasecmp( chr($ASCII), chr($ASCII + 32), 1 ) );
//comparing uppercase letter with
//corresponding lowercase letter; exp: int(0)
echo '<br>';
}
echo "<br><br><br>Passing lower-case letters for 'str1':<br>";
for($ASCII = 97; $ASCII <= 122; $ASCII++) {
var_dump( strncasecmp( chr($ASCII), chr($ASCII), 1 ) );
//comparing lowercase letter with
//corresponding lowercase letter; exp: int(0)
echo '<br>';
var_dump( strncasecmp( chr($ASCII), chr($ASCII - 32), 1 ) );
//comparing lowercase letter with
//corresponding uppercase letter; exp: int(0)
echo '<br>';
}
?>
<?php
/* Test strncasecmp() function with
various double quoted strings for
'str1', 'str2'
*/
echo "Test strncasecmp() function:
with double quoted strings.<br>";
$strings = array(
"Hello, World",
"hello, world",
"HELLO, WORLD",
"Hello, World\n",
"Hello".chr(0)."World"
);
/* loop through to compare each
string with the other string */
$count = 1;
for($index1 = 0; $index1 < count($strings); $index1++) {
echo "<br><br>Iteration: $count<br>";
for($index2 = 0; $index2 < count($strings); $index2++) {
var_dump( strncasecmp( $strings[$index1],
$strings[$index2],
(strlen($strings[$index1]) + 1) ) );
echo "<br>";
}
$count ++;
}
?>
<?php
/* Test strncasecmp() with various lengths */
echo "Test strncasecmp() function: with different lengths.<br><br>";
/* definitions of required variables */
$str1 = "Hello, World\n";
$str2 = "Hello, world\n";
/* loop through to compare the strings,
for various length values */
for($len = strlen($str1); $len >= 0; $len--) {
var_dump( strncasecmp($str1, $str2, $len) );
echo '<br>';
}
?>
<?php
/* Test strncasecmp() function with
binary values passed to
'str1' & 'str2' */
echo "Test strncasecmp() function: with binary inputs.<br>";
/* A binary function should work with all
256 characters that a character(8-bit)
can take */
echo "<br><br>Checking with all
256 characters given,
in binary format:<br>";
/* loop through to get all 256 character's
equivalent binary value, and check working
of strncasecmp() */
$count = 1;
for($ASCII = 0; $ASCII <= 255; $ASCII++) {
$str1 = decbin($ASCII);
//ASCII value in binary form
$str2 = decbin( ord( chr($ASCII) ) );
//Getting equivalent ASCII value
//for the character in binary form
echo "<br><br>Iteration: $count:<br>";
var_dump( strncasecmp($str1, $str2, 8) );
//comparing all the 8-bits; expected: int(0)
echo "<br>";
var_dump( strncasecmp($str1, $str2, 4) );
//comparing only 4-bits; expected: int(0)
echo "<br>";
$count++;
}
echo "<br><br> Checking with out of
character's range,
given in binary format:<br>";
$str1 = decbin(256);
$str2 = decbin( ord( chr(256) ));
var_dump( strncasecmp($str1, $str2, 8) );
//comparing all the 8-bits; expected: int(1)
?>
<?php
/* Test strncasecmp() function with null
terminated strings and binary values
passed to 'str1' & 'str2' */
echo "Test strncasecmp() function:
with null terminated strings and binary inputs.<br>";
function str_mdump($one, $two, $three) {
echo "<br>$one<br>and<br>$two<br>and<br>$three<br><br>Returned value: ";
var_dump(strncasecmp($one, $two, $three));
echo '<br>- - - - - - -<br>';
}
/* A binary function should not expect
a null terminated string, and it should
treat input as a raw stream of data */
echo '<br>- - - - - - -<br>';
$str1 = "Hello\0world";
$str2 = "Hello\0";
$str3 = "Hello,".chr(0)."world";
str_mdump($str1, $str2, 12);
str_mdump($str3, "Hello,world", 12);
?>
<?php
/* Test strncasecmp() function with various
single quoted strings for 'str1', 'str2' */
echo "Test strncasecmp() function:
with single quoted strings.<br>";
$strings = [
'Hello, World',
'hello, world',
'HELLO, WORLD',
'Hello, World\n'
];
/* loop through to compare each
string with the other string */
$count = 1;
for($index1 = 0; $index1 < count($strings); $index1++) {
echo "<br><br>Iteration: $count<br>";
for($index2 = 0; $index2 < count($strings); $index2++) {
echo "After:<br>strncasecmp( '$strings[$index1]',
'$strings[$index2]',
(strlen('$strings[$index1]') + 1) )
<br>Result value: ";
var_dump( strncasecmp( $strings[$index1],
$strings[$index2],
(strlen($strings[$index1]) + 1) ) );
echo '<br><br>';
}
$count ++;
}
?>
<?php
/* Test strncasecmp() function with
here-doc strings for 'str1', 'str2' */
echo "Test strncasecmp() function:
with here-doc strings.<br><br>";
/* multi line heredoc string */
$multi_line_str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
/* identifier name
contains underscore */
$identifier_str1 = <<<identifier_str1
Example of heredoc
string, whose identifier
having underscore("_")
& numeric value.
identifier_str1;
/* identifier name starts
with underscore */
$identifier_str2 = <<<_identifier_str2
Hello, World
hello, world
_identifier_str2;
/* string containing
control character */
$control_char_str = <<<EOD
Hello, World\n
Hello\0World
EOD;
/* heredoc string with
quote chars & slash */
$quote_char_string = <<<EOD
it's bright,but i cann't see it.
"things in double quote"
'things in single quote'
this\line is /with\slashs
EOD;
/* heredoc string with
blank line */
$blank_line = <<<EOD
EOD;
/* empty heredoc string */
$empty_string = <<<EOD
EOD;
$strings = array(
$multi_line_str,
$identifier_str1,
$identifier_str2,
$control_char_str,
$quote_char_string,
$blank_line,
$empty_string
);
/* loop through to compare
the strings */
$index2 = count($strings);
for($index1 = 0; $index1 < count($strings); $index1++) {
$index2--;
echo "After:<br>strncasecmp( '$strings[$index1]',
'$strings[$index1]',
strlen('$strings[$index1]') )
<br>Result value: ";
var_dump( strncasecmp( $strings[$index1],
$strings[$index1],
strlen($strings[$index1]) ) );
echo "<br><br>After:<br>strncasecmp( $strings[$index1],
$strings[$index2],
strlen($strings[$index1]) )
<br>Result value: ";
var_dump( strncasecmp( $strings[$index1],
$strings[$index2], strlen($strings[$index1]) ) );
echo '<br><br>';
}
?>
<?php
echo "Testing strncasecmp() function:
error conditions.<br>";
$str1 = 'string_val';
$str2 = 'string_val';
echo "<br>Testing strncasecmp() function
with invalid argument:<br>";
$len = -10;
try {
var_dump( strncasecmp($str1, $str2, $len) );
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
?>