PHP substr()ฟังก์ชั่น
ตัวอย่าง
กลับ "โลก" จากสตริง:
<?php
echo substr("Hello world",6);
?>
ความหมายและการใช้งาน
ฟังก์ชัน substr() ส่งกลับส่วนหนึ่งของสตริง
ไวยากรณ์
substr(string,start,length)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
string | Required. Specifies the string to return a part of |
start | Required. Specifies where to start in the string
|
length | Optional. Specifies the length of the returned string. Default is to the end of the string.
|
รายละเอียดทางเทคนิค
คืนมูลค่า: | ส่งกลับส่วนที่แยกออกมาของสตริง หรือ FALSE เมื่อล้มเหลว หรือสตริงว่าง |
---|---|
รุ่น PHP: | 4+ |
บันทึกการเปลี่ยนแปลง: | PHP 7.0 - หากstring = start (เป็นตัวอักษรยาว) จะส่งคืนสตริงว่าง เวอร์ชันก่อนหน้าส่งคืน FALSE PHP 5.2.2 - 5.2.6 - หาก การ เริ่มต้นมีตำแหน่งของการตัดทอนเป็นลบ FALSE จะถูกส่งคืน เวอร์ชันอื่นรับสตริงตั้งแต่เริ่มต้น |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
การใช้พารามิเตอร์เริ่มต้นที่มีจำนวนบวกและลบต่างกัน:
<?php
echo substr("Hello world",10)."<br>";
echo substr("Hello world",1)."<br>";
echo substr("Hello world",3)."<br>";
echo substr("Hello world",7)."<br>";
echo substr("Hello world",-1)."<br>";
echo substr("Hello world",-10)."<br>";
echo substr("Hello world",-8)."<br>";
echo substr("Hello world",-4)."<br>";
?>
ตัวอย่าง
การใช้พารามิเตอร์เริ่มต้นและความยาวที่มีจำนวนบวกและลบต่างกัน:
<?php
echo substr("Hello world",0,10)."<br>";
echo substr("Hello world",1,8)."<br>";
echo substr("Hello world",0,5)."<br>";
echo substr("Hello world",6,6)."<br>";
echo substr("Hello world",0,-1)."<br>";
echo substr("Hello world",-10,-2)."<br>";
echo substr("Hello world",0,-6)."<br>";
?>
❮ การอ้างอิงสตริง PHP