สมมติว่าต่อไปนี้คือสตริงของเรา −
$sentence="This is my first PHP program";
เราต้องการผลลัพธ์ต่อไปนี้ -
This is my first PHP ... gram
เราย่อสตริงด้วย “…” สำหรับสิ่งนี้ ให้ใช้แนวคิดของ substring() และตั้งค่าเงื่อนไขโดยตรวจสอบจำนวนคำ −
ตัวอย่าง
<!DOCTYPE html>
<html>
<body>
<?php
$sentence="This is my first PHP program";
if (strlen($sentence) >= 13) {
echo substr($sentence, 0, 20). " ... " . substr($sentence, -4);
} else {
echo $sentence;
}
?>
</body>
</html> ผลลัพธ์
This is my first PHP ... gram