Hướng dẫn substr_compare in php

❮ PHP String Reference

Example

Compare two strings:

Try it Yourself »

Definition and Usage

The substr_compare[] function compares two strings from a specified start position.

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

Syntax

substr_compare[string1,string2,startpos,length,case]

Parameter Values

ParameterDescription
string1 Required. Specifies the first string to compare
string2 Required. Specifies the second string to compare
startpos Required. Specifies where to start comparing in string1. If negative, it starts counting from the end of the string
length Optional. Specifies how much of string1 to compare
case Optional. A boolean value that specifies whether or not to perform a case-sensitive compare:
  • FALSE - Default. Case-sensitive
  • TRUE - Case-insensitive

Technical Details

Return Value:PHP Version:Changelog:
This function returns:
  • 0 - if the two strings are equal
  • 0 - if string1 [from startpos] is greater than string2
If length is equal or greater than length of string1, this function returns FALSE.
5+
As of PHP 5.5.11 - The length parameter can be 0.
As of PHP 5.1, it is now possible to use a negative startpos.

More Examples

Example

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

Try it Yourself »

Example

Using all parameters:

Try it Yourself »

Example

Different return values:

Try it Yourself »

❮ PHP String Reference


❮ Tham chiếu chuỗi PHP

Thí dụ

So sánh hai chuỗi:

Định nghĩa và Cách sử dụng

Hàm substr_compare [] so sánh hai chuỗi từ một vị trí bắt đầu được chỉ định.

Mẹo: Hàm này an toàn nhị phân và có phân biệt chữ hoa chữ thường.

Cú pháp

substr_compare[string1,string2,startpos,length,case]

Giá trị tham số

ParameterDescription
string1 Required. Specifies the first string to compare
string2 Required. Specifies the second string to compare
startpos Required. Specifies where to start comparing in string1. If negative, it starts counting from the end of the string
length Optional. Specifies how much of string1 to compare
case Optional. A boolean value that specifies whether or not to perform a case-sensitive compare:
  • FALSE - Default. Case-sensitive
  • TRUE - Case-insensitive

Chi tiết kỹ thuật

Giá trị trả lại:Phiên bản PHP:Changelog:
Hàm này trả về:
  • 0 - nếu hai chuỗi bằng nhau
  • 0 - nếu string1 [từ startpos] lớn hơn string2
Nếu độ dài bằng hoặc lớn hơn độ dài của string1, hàm này trả về FALSE.
5+
Đối với PHP 5.5.11 - Tham số độ dài có thể là 0.
Đối với PHP 5.1, giờ đây có thể sử dụng startpos phủ định.

Các ví dụ khác

Thí dụ

So sánh hai chuỗi, khi vị trí bắt đầu trong chuỗi1 để so sánh là thứ 6:

Thí dụ

Sử dụng tất cả các tham số:

Thí dụ

Các giá trị trả về khác nhau:

❮ Tham chiếu chuỗi PHP


Chủ Đề