substr_compare 


string apg

COMPARE two strings from an offset, up to length characters.





If $offset < 0 the counting starts from the end of the STRING.

The default $length value is the largest of the length of the $str compared to the length of $main_str minus $offset.

This function returns a NEGATIVE value if $main_str from position $offset is LESS than $str length.

This function returns a POSITIVE value if $main_str from position $offset is GREATER than $str length.

If $offset is EQUAL to, (prior to PHP 7.2.18, 7.3.5), or GREATER than the length of $main_str, or the $length is set and is less than 0, (or, prior to PHP 5.5.11, less than 1) this function prints a WARNING and returns FALSE.

This function is binary-safe

In PHP, some functions are marked as binary-safe, it means that the functions works correctly even when you pass binary data.



<?php

int substr_compare 
str $main_str 
                                 
str $str 
                                 
int $offset [, 
                                 
int $length [, 
                                
bool $case_insensitivity FALSE ]] ) 


where,

$main_str The main STRING being compared

$str 
The secondary STRING being compared

$offset 
The offset where to start counting

$length 
The length of the comparisson

$case_insensitivity 
To control the comparisson case
                                ( 
SEE the TABLE )
?>

$main_str


The main STRING being compared.



$str


The secondary STRING being compared.



$offset


The $offset where to start counting.



$length


The $length where to start the comparisson.



$case_insensitivity

VALUE MEANING
FALSE The comparisson will be case sensitive.
TRUE The comparisson will be case insensitive.
ed48


  1 EXERCISE   

<table width="100%" cellspacing="5" 
cellpadding="5" border="1">
<tbody><tr>
<td width="12%">$offset</td>
<td width="9%">RETURNS</td>
<td width="56%">SECTION of $main_str 
from $offset to $length</td>
<td width="23%">$str</td>
</tr>
<?php

$main_str01 
"NOT CONSTITUTIONAL";
$str01 "CONSTITUTIONAL";
// UPPERCASE

for ($offset01 0
      
$offset01 <= strlen($main_str01) - 1;
      
$offset01++)
{
$comp01 substr_compare($main_str01$str01$offset01); 
// Without consider the length of $str01 

echo '<tr><td>' $offset01 '</td><td>' $comp01 
'</td><td>' substr($main_str01$offset01) . 
'</td><td>' $str01 '</td></tr>';
}

?>
<tr><td colspan="4">ed48</td></tr></tbody></table>


  2 EXERCISE   

<table width="100%" cellspacing="5" 
cellpadding="5" border="1">
<tbody><tr>
<td width="12%">$offset</td>
<td width="9%">RETURNS</td>
<td width="56%">SECTION of $main_str 
from $offset to $length</td>
<td width="23%">$str</td>
</tr>
<?php

$main_str02 
"NOT CONSTITUTIONAL";
$str02 "CONSTITUTIONAL";
// UPPERCASE

for ($offset02 0
       
$offset02 <= strlen($main_str02) - 1
       
$offset02++)
{
$comp02 substr_compare($main_str02$str02
                              
$offset02strlen($str02)); 
// Considering the length of $str02

echo '<tr><td>' $offset02 '</td><td>' $comp02 
'</td><td>' substr($main_str02$offset02) . 
'</td><td>' $str02 '</td></tr>';
}

?>
<tr><td colspan="4">ed48</td></tr></tbody></table>


  3 EXERCISE   

<table width="100%" cellspacing="5" 
cellpadding="5" border="1">
<tbody><tr>
<td width="12%">$offset</td>
<td width="9%">RETURNS</td>
<td width="56%">SECTION of $main_str 
from $offset to $length</td>
<td width="23%">$str</td>
</tr>
<?php

$main_str03 
"NOT CONSTITUTIONAL";
$str03 "constitutional";
// lowercase

for ($offset03 0
        
$offset03 <= strlen($main_str03) - 1
        
$offset03++)
{
$comp03 substr_compare($main_str03
                                    
$str03$offset03); 
// Without consider the length of $str03 

echo '<tr><td>' $offset03 '</td><td>' $comp03 
'</td><td>' substr($main_str03$offset03) . 
'</td><td>' $str03 '</td></tr>';
}

?>
<tr><td colspan="4">ed48</td></tr></tbody></table>


  4 EXERCISE   

<table width="100%" cellspacing="5" 
cellpadding="5" border="1">
<tbody><tr>
<td width="12%">$offset</td>
<td width="9%">RETURNS</td>
<td width="56%">SECTION of $main_str 
from $offset to $length</td>
<td width="23%">$str</td>
</tr>
<?php

$main_str04 
"Não é sempre que 
se pode confiar em alguém!"
;
/* It is not always
you can trust someone! */

// May experience problems 
// in languages other than English

$str04 "SEMPRE";
// UPPERCASE

for ($offset04 0
       
$offset04 <= strlen($main_str04) - 1
       
$offset04++)
{
$comp04 substr_compare($main_str04
                                   
$str04$offset04); 
// Without consider the length of $str04 

echo '<tr><td>' $offset04 '</td><td>' $comp04 
'</td><td>' substr($main_str04$offset04) . 
'</td><td>' $str04 '</td></tr>';
}

?>
<tr><td colspan="4">ed48</td></tr></tbody></table>


  5 EXERCISE   

<?php

var_dump
(substr_compare("abcde""df", -2) < 0);
echo 
'<br><br>';
var_dump(substr_compare("abcde""df", -2null) < 0);
echo 
'<br><br>';
var_dump(substr_compare("abcde""bc"12));
echo 
'<br><br>';
var_dump(substr_compare("abcde""bcg"12));
echo 
'<br><br>';
var_dump(substr_compare("abcde""BC"12true));
echo 
'<br><br>';
var_dump(substr_compare("abcde""bc"13) > 0);
echo 
'<br><br>';
var_dump(substr_compare("abcde""cd"12) < 0);
echo 
'<br><br>';
var_dump(substr_compare("abcde""abc"51));
echo 
'<br><br>';
var_dump(substr_compare("abcde""abcdef", -1010) < 0);
echo 
'<br><br>';
var_dump(substr_compare("abcde""abc"00));
echo 
"<br><br>Test<br><br><br>";

try {
    
substr_compare("abcde""abc"0, -1);
} catch (
\ValueError $e) {
    echo 
$e->getMessage() . "<br>";
}
echo 
'<br><br>';
var_dump(substr_compare("abcde""abc", -1NULL, -5) > 0);

?>