Hướng dẫn substr_compare php - substr_compare php

Hàm substr_compare() có tác dụng so sánh 1 đoạn của chuỗi này với một chuỗi khác. Ta có thể hiểu rằng hàm sẽ lấy một đoạn chuỗi ban đầu rồi đem so sánh chuỗi con đó với một chuỗi khác.

Hướng dẫn substr_compare php - substr_compare php

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.freetuts.net, không được copy dưới mọi hình thức.

Cú pháp

Cú pháp: substr_compare($main_str, $str, $pos, $lent);: substr_compare($main_str, $str, $pos, $lent);

Trong đó::

  • $main_str là chuỗi thứ nhất.
  • $str là chuỗi thứ 2.
  • $pos là vị trí bắt đầu so sánh ở $main_str.
  • $lent là số kí tự tính từ $pos của chuỗi $main_str sẽ đem so sánh với $str.

Ví dụ

Đây là ví dụ mình tham khảo trên trang chủ php.net:

Bài viết này được đăng tại [free tuts .net]

Code

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

Kết quả

0
0
0
0
1
-1

Warning: substr_compare(): The start position cannot exceed initial string length in C:\xampp\htdocs\test\index.php on line 8

Tham khảo: php.net

❮ Tham chiếu chuỗi PHP

Thí dụ

So sánh hai chuỗi:

echo substr_compare("Hello world","Hello world",0);
?>


Đị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. 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:Hàm này trả về:
  • 0 - nếu hai chuỗi bằng nhau
  • <0 - nếu string1 (từ startpos) nhỏ hơn string2
  • Đị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.5+
Changelog: substr_compare(string1,string2,startpos,length,case) độ dài có thể là 0.
Đối với PHP 5.1, giờ đây có thể sử dụng startpos phủ định.

Giá trị tham số

Thí dụ

So sánh hai chuỗi:

echo substr_compare("Hello world","world",6);
?>

Thí dụ

So sánh hai chuỗi:

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);
?>

Thí dụ

So sánh hai chuỗi:

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
?>


❮ Tham chiếu chuỗi PHP