如何限制下面的錨文本以僅顯示50個字符?限制一個變量只顯示50個字符
echo '<td style="" class="pointlink"><span class="pointlink"><a href="http://'.$rowad["1site"].'">'.$rowad["1site"].'</a></td>';
如何限制下面的錨文本以僅顯示50個字符?限制一個變量只顯示50個字符
echo '<td style="" class="pointlink"><span class="pointlink"><a href="http://'.$rowad["1site"].'">'.$rowad["1site"].'</a></td>';
可以使用substr
功能,如
echo '<td style="" class="pointlink"><span class="pointlink"><a href="http://'.$rowad["1site"].'">'.substr($rowad["1site"],0,50).'</a></td>';
可以使用substr
功能砍下文本50個字符
function cut_text($text, $len) {
return strlen($text) > $len ?
substr($text, 0, $len) + '...' :
$text;
}
echo '<td style="" class="pointlink"><span class="pointlink"><a href="http://'.$rowad["1site"].'">' . cut_text($rowad["1site"], 50) . '</a></td>';
顯示截斷結尾的省略號在哪裏? ;-) – RobertMaysJr
不錯 - 但你應該使用'&hellip;'獲得一個真正的'...'而不是'...' - 關鍵是要節省空間畢竟:-)(大聲笑 - 在編輯框中看起來更大......) – RobertMaysJr
或者,你呢ld use CSS:
.pointlink {
width: 150px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
根據需要調整寬度,並自動使用省略號來截斷文本。
Upvoting - 因爲你會有省略號顯示截斷,這可以根據頁面的大小動態調整大小。 – RobertMaysJr
可愛,但效率低下,並有嚴重的瀏覽器支持問題。 – 2012-09-03 21:11:22
受所有主流瀏覽器支持:http://www.w3schools.com/cssref/css3_pr_text-overflow.asp – RobertMaysJr
substr()是你想要的函數 – 2012-09-03 21:01:34