0
我試圖讓Facebook的show more
行爲起作用。 我希望它修剪字符串,如果:類似Facebook的「顯示更多」按鈕,用於帶有網址的字符串
- 其長度超過200個字符。
- 有5個以上
/n
發生。
這聽起來很簡單,我已經有一個初步的功能(僅按長度這樣做,我還沒有實現的/n
事件尚未):
function contentShowMore($string, $max_length) {
if(mb_strlen($string, 'utf-8') <= $max_length) {
return $string; // return the original string if haven't passed $max_length
} else {
$teaser = mb_substr($string, 0, $max_length); // trim to max length
$dots = '<span class="show-more-dots"> ...</span>'; // add dots
$show_more_content = mb_substr($string, $max_length); // get the hidden content
$show_more_wrapper = '<span class="show-more-content">'.$show_more_content.'</span>'; // wrap it
return $teaser.$dots.$show_more_wrapper; // connect all together for usage on HTML.
}
}
的問題是,該字符串可能包括網址,所以它打破了它們。我需要找到一種方法來製作功能show-more
按鈕,它可以檢查長度,換行符並且不會切斷網址。
謝謝!
實施例:
輸入:contentShowMore("hello there http://google.com/ good day!", 20)
。
輸出:
hello there http://g
<span class="show-more-dots"> ...</span>
<span class="show-more-content">oogle.com/ good day!</span>
輸出我想:
hello there http://google.com/
<span class="show-more-dots"> ...</span>
<span class="show-more-content"> good day!</span>
你能分享你的意見和期望的結果嗎? –
沒有特定的輸入和輸出,因爲此功能將運行很多。但我可以舉一個例子。我會將其添加到我的問題,堅持 –
我可以問你是否傳遞第二個參數爲'20',以及你如何得到它,我會幫助你,爲了更好的理解我只想了解你的問題。 –