PHP wordwrap()ฟังก์ชั่น
ตัวอย่าง
ตัดสตริงเป็นบรรทัดใหม่เมื่อถึงความยาวที่กำหนด:
<?php
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15,"<br>\n");
?>
ความหมายและการใช้งาน
ฟังก์ชัน wordwrap() ตัดสตริงเป็นบรรทัดใหม่เมื่อถึงความยาวที่กำหนด
หมายเหตุ:ฟังก์ชันนี้อาจเว้นช่องว่างสีขาวไว้ที่จุดเริ่มต้นของบรรทัด
ไวยากรณ์
wordwrap(string,width,break,cut)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
string | Required. Specifies the string to break up into lines |
width | Optional. Specifies the maximum line width. Default is 75 |
break | Optional. Specifies the characters to use as break. Default is "\n" |
cut |
Optional. Specifies whether words longer than the specified width should be wrapped:
|
รายละเอียดทางเทคนิค
คืนมูลค่า: | ส่งกลับสตริงที่แบ่งเป็นบรรทัดเมื่อสำเร็จ หรือ FALSE เมื่อล้มเหลว |
---|---|
รุ่น PHP: | 4.0.2+ |
บันทึกการเปลี่ยนแปลง: | เพิ่มพารามิเตอร์การตัด ใน PHP 4.0.3 |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
ใช้พารามิเตอร์ทั้งหมด:
<?php
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15,"<br>\n",TRUE);
?>
ตัวอย่าง
ตัดสตริงเป็นบรรทัดใหม่:
<?php
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15);
?>
เอาต์พุต HTML ของโค้ดด้านบนจะเป็น (View Source):
<!DOCTYPE html>
<html>
<body>
An example of a
long word is:
Supercalifragulistic
</body>
</html>
ผลลัพธ์ของเบราว์เซอร์ของโค้ดด้านบนจะเป็น:
An example of a long word is: Supercalifragulistic
❮ การอ้างอิงสตริง PHP