Giả sử chuỗi sau:
$sentence = "This is my first PHP program";
Muốn kết quả:
This is my first PHP ... gram
Rút gọn chuỗi với "...". Dùng substr() và kiểm tra số từ.
Ví dụ
<!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>Kết quả
This is my first PHP ... gram