如何剪切單詞並在達到4或5個單詞後添加「...」?PHP:如何剪切單詞並添加「...」
下面的代碼表示我做了基於字符的單詞cutb,但現在我需要它通過單詞。
目前我有這樣的代碼:
if(strlen($post->post_title) > 35)
{
$titlep = substr($post->post_title, 0, 35).'...';
}
else
{
$titlep = $post->post_title;
}
,這是標題的輸出:
if ($params['show_title'] === 'true') {
$title = '<h3 class="wp-posts-carousel-title">';
$title.= '<a href="' . $post_url . '" title="' . $post->post_title . '">' . $titlep . '</a>';
$title.= '</h3>';
}
我建議使用CSS,並把它在客戶端,這是一個演示文稿的問題:https://開頭的CSS-技巧.com/snippets/css/truncate-string-with-ellipsis/ – user2182349
同意user2182349。雖然下面提供的純粹的PHP答案可以工作,但CSS解決方案會更加理想和靈活。 – cteski
NAH ......在我剪下4-5個字後,它必須有「...」......但是非常感謝你的建議。我只是需要這是合乎邏輯的。 –