PHP substr_compare() Function


The substr_compare() function is used to compares two strings from a specified start position to specified end position.This function is binary-safe and optionally case-sensitive.

Syntax
substr_compare(string1,string2,startpos,length,case)
Parameters
  • String1 - (Required) Specify the first string to compare.
  • String2 - (Required) Specify the second string to compare.
  • Startpos - (Required) Specify where to start comparing in string1 and string2. -it provides the start position for comparison.
  • length - (Required) Specify how much of string1 to compare
  • case - (Required) specify the parameter boolean value that specify whether or not to perform a case-sensitive compare:
    FALSE -Case-sensitive(Default value)
    TRUE - Case-insensitive
Example
<?php
    echo substr_compare("Good Morning","Good Morning",0);
?>

Output

0
Example
<?php
    echo substr_compare("morning","or",1,2)."<br>";
    echo substr_compare("good","od",2,3)."<br>";
    echo substr_compare("morning","OR",1,3,TRUE)."<br>";
    echo substr_compare("good","og",1,3)."<br>";
?>

Output

0
0
1
2048

Prev Next