Jump to: navigation, search

Php substr compare

From w3cyberlearnings

Contents

PHP function substr_compare

This function compares two strings.

Syntax substr_compare

  • string1: main string input
  • string2: second string input
  • startpos: specifies where to start comparing in string1 and string2
  • length: specifies length of the comparision
  • case: True (case insensitive), Default is FALSE (case sensitive)
substr_compare(string1, string2, startpos, length, case)

Return

  • 0: string1 and string2 are equal
  • > 0: string1 is greater than string2
  • < 0: string1 is less than string2

Example 1

  • string1: abcde
  • string2: cde
  • startpos: 2
  • length: 3
  • return: 0 (string1=string2)
<?php
echo substr_compare("abcde", "cde", 2, 3);
?>

Output

0

Example 2

<?php
echo substr_compare("abcde", "CDE", 2, 3, FALSE); //1
echo substr_compare("abcde", "CDE", 2, 3, TRUE);  //0
?>

Output

<?php
echo substr_compare("abcde", "bc", 1, 2); // 0
echo substr_compare("abcde", "de", -2, 2); // 0
echo substr_compare("abcde", "bcg", 1, 2); // 0
echo substr_compare("abcde", "BC", 1, 2, true); // 0
echo substr_compare("abcde", "bc", 1, 3); // 1
echo substr_compare("abcde", "cd", 1, 2); // -1
echo substr_compare("abcde", "abc", 5, 1); // warning
?>

Related Functions


Navigation
Web
SQL
MISC
References