Hàm substr_compare() của PHP
Ví dụ
So sánh hai chuỗi:
<?php
echo substr_compare("Hello world","Hello world",0);
?>
Hãy tự mình thử »Định nghĩa và cách sử dụng
Hàm substr_compare() so sánh hai chuỗi từ vị trí bắt đầu được chỉ định.
Mẹo: Hàm này an toàn nhị phân và phân biệt chữ hoa chữ thường tùy chọn.
Cú pháp
substr_compare( string1,string2,startpos,length,case )
Giá trị tham số
Parameter | Description |
---|---|
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:
|
Chi tiết kỹ thuật
Giá trị trả về: | Hàm này trả về:
|
---|---|
Phiên bản PHP: | 5+ |
Nhật ký thay đổi: | Kể từ PHP 5.5.11 - Tham số độ dài có thể là 0. Kể từ PHP 5.1, giờ đây bạn có thể sử dụng điểm bắt đầu phủ định. |
Thêm ví dụ
Ví dụ
So sánh hai chuỗi, khi vị trí bắt đầu trong chuỗi 1 để so sánh là thứ 6:
<?php
echo substr_compare("Hello world","world",6);
?>
Hãy tự mình thử »Ví dụ
Sử dụng tất cả các tham số:
<?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);
?>
Hãy tự mình thử »Ví dụ
Giá trị trả về khác nhau:
<?php
echo substr_compare("Hello world!","Hello world!",0); // the 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); // str1 is less than str2
?>
Hãy tự mình thử »❮ Tham chiếu chuỗi PHP