PHP substr_compare()ฟังก์ชั่น
ตัวอย่าง
เปรียบเทียบสองสตริง:
<?php
echo substr_compare("Hello world","Hello world",0);
?>
ความหมายและการใช้งาน
ฟังก์ชัน substr_compare() เปรียบเทียบสองสตริงจากตำแหน่งเริ่มต้นที่ระบุ
เคล็ดลับ:ฟังก์ชันนี้มีความปลอดภัยแบบไบนารีและสามารถเลือกกรณีได้
ไวยากรณ์
substr_compare(string1,string2,startpos,length,case)
ค่าพารามิเตอร์
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:
|
รายละเอียดทางเทคนิค
คืนมูลค่า: | ฟังก์ชันนี้ส่งคืน:
|
---|---|
รุ่น PHP: | 5+ |
บันทึกการเปลี่ยนแปลง: | ตั้งแต่ PHP 5.5.11 - พารามิเตอร์ ความยาวสามารถเป็น 0 ได้ สำหรับ PHP 5.1 ตอนนี้สามารถใช้startpos เชิงลบได้แล้ว |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
เปรียบเทียบสองสตริง เมื่อตำแหน่งเริ่มต้นใน string1 สำหรับการเปรียบเทียบคือลำดับที่ 6:
<?php
echo substr_compare("Hello world","world",6);
?>
ตัวอย่าง
ใช้พารามิเตอร์ทั้งหมด:
<?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);
?>
ตัวอย่าง
ค่าส่งคืนที่แตกต่างกัน:
<?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
?>
❮ การอ้างอิงสตริง PHP