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
  • Escape metacharacter set quotemeta

    quotemeta

    Escapemetacharacters
  • Write the formatted string to the stream fprintf

    fprintf

    Writetheformattedstr
  • Find any one of a set of characters in a string - Return a substring that starts with the found character strpbrk

    strpbrk

    Findanyoneofasetofch
  • Set the first character of the string to lowercase lcfirst

    lcfirst

    Setthefirstcharacter
  • Parse strings into multiple variables parse_str

    parse_str

    Parsestringsintomult
  • Insert HTML newline tag before all new lines of a string nl2br

    nl2br

    InsertHTMLnewlinetag
  • Substring replacement str_replace

    str_replace

    Substringreplacement
  • Generate single-byte strings from numbers chr

    chr

    Generatesingle-bytes
Popular Articles