Cập nhật ngày 13/05/2022
Hàm này sẽ cắt ra số từ theo số lượng cho trước
<?php function word_cut(string $string, int $word = 35): string { $split = explode(' ', $string); $word_count = count($split); // Neu so tu cho phep nho hon thuc te if ($word < $word_count) { $output = array_slice($split, 0, $word); $output = implode(' ', $output); $output .= '...'; } else { $output = implode(' ', $split); } return $output; }
Cách dùng:
$str = 'a b c d e f g h'; echo word_cut($str, 3);