Current Location: Home> Function Categories> substr_compare

substr_compare

Binary safe comparison string (compare the specified length from offset position)
Name:substr_compare
Category:String
Programming Language:php
One-line Description:Compare two strings from the specified starting position (binary safe and selective case sensitive).

Definition and usage

substr_compare() function compares two strings from the specified starting position.

Tip: This function is binary safe and selectively case sensitive.

Example

Example 1

Compare two strings:

 <?php
echo substr_compare ( "Hello world" , "Hello world" , 0 ) ;
?>

Try it yourself

Example 2

Compare two strings, when the start position for comparison in string1 is 6:

 <?php
echo substr_compare ( "Hello world" , "world" , 6 ) ;
?>

Try it yourself

Example 3

Use all parameters:

 <?php
echo substr_compare ( "world" , "or" , 1 , 2 ) ;
echo substr_compare ( "world" , "ld" , - 2 , 2 ) ;
echo substr_compare ( "world" , "orl" , 1 , 2 ) ;
echo substr_compare ( "world" , "OR" , 1 , 2 , TRUE ) ;
echo substr_compare ( "world" , "or" , 1 , 3 ) ;
echo substr_compare ( "world" , "rl" , 1 , 2 ) ;
?>

Try it yourself

Example 4

Different return values:

 <?php
echo substr_compare ( "Hello world!" , "Hello world!" , 0 ) ; // Two strings are equal
echo substr_compare ( "Hello world!" , "Hello" , 0 ) ; // string1 is greater than string2
echo substr_compare ( "Hello world!" , "Hello world! Hello!" , 0 ) ; // string1 is less than string2
?>

Try it yourself

grammar

 substr_compare ( string1 , string2 , startpos , length , case )
parameter describe
string1 Required. Specifies the first string to be compared.
string2 Required. Specifies the second string to be compared.
startpos Required. Specifies where to start the comparison in string1 . If it is a negative number, count starts from the end of the string.
length Optional. Specifies how many characters in string1 are compared (number of characters).
case

Optional. Boolean value, specifying whether to perform case-sensitive comparisons:

  • FALSE - Default. case sensitive
  • TRUE - case insensitive
Similar Functions
Popular Articles