PHP compares two strings of function Substr_compare () from a specified starting position

Source: Internet
Author: User

Instance

Comparison of two strings:

<?phpecho substr_compare ("Hello World", "Hello World", 0);? >

Definition and usage

The Substr_compare () function compares two strings from a specified starting position.

Tip: This function is binary safe and selective case-sensitive.

Grammar

Substr_compare (String1,string2,startpos,length,case)
Parameters Describe
String1 Necessary. Specifies the first string to compare.
string2 Necessary. Specifies a second string to compare.
Startpos Necessary. Specify where in the string1 to begin the comparison. If negative, the count starts at the end of the string.
Length Optional. Specifies the number of characters to participate in the comparison in string1.
Case Optional. A Boolean value that specifies whether a case-sensitive comparison is performed:
  • FALSE-Default. Case sensitive

  • TRUE-Case insensitive

technical details

return value: This function returns:
  • 0-if two strings are equal

  • <0-if string1 (from start position startpos) is less than string2

  • >0-if string1 (startpos from start) is greater than string2

if length is greater than or equal to the length of string1, the function returns FALSE.
php version: 5+
update log: from PHP 5.1, negative startpos are allowed.

More examples

Example 1

Compares two strings when the starting position for comparison in string1 is 6 o'clock:

<?phpecho substr_compare ("Hello World", "World", 6);? >

Example 2

Use all the parameters:

<?phpecho Substr_compare ("World", "or",;); echo Substr_compare ("World", "LD", -2,2); echo Substr_compare ("World", "ORL",); echo Substr_compare ("World", "OR", 1,2,true); echo Substr_compare ("World", "or", 1, 3); echo Substr_compare ("World", "RL"); >

Example 3

Different return values:

<?phpecho substr_compare ("Hello world!", "Hello world!", 0); The strings is Equalecho substr_compare ("Hello world!", "Hello", 0); String1 is greater than String2echo substr_compare ("Hello world!", "Hello world! Hello! ", 0); STR1 is less than str2?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.