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',以及你如何得到它,我会帮助你,为了更好的理解我只想了解你的问题。 –