2012-09-03 29 views
1

如何限制下面的錨文本以僅顯示50個字符?限制一個變量只顯示50個字符

echo '<td style="" class="pointlink"><span class="pointlink"><a href="http://'.$rowad["1site"].'">'.$rowad["1site"].'</a></td>'; 
+2

substr()是你想要的函數 – 2012-09-03 21:01:34

回答

2

可以使用substr功能,如

echo '<td style="" class="pointlink"><span class="pointlink"><a href="http://'.$rowad["1site"].'">'.substr($rowad["1site"],0,50).'</a></td>';

3

可以使用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>'; 
+0

顯示截斷結尾的省略號在哪裏? ;-) – RobertMaysJr

+0

不錯 - 但你應該使用'&hellip;'獲得一個真正的'...'而不是'...' - 關鍵是要節省空間畢竟:-)(大聲笑 - 在編輯框中看起來更大......) – RobertMaysJr

7

substr

或者,你呢ld use CSS:

.pointlink { 
    width: 150px; 
    white-space: nowrap; 
    overflow: hidden; 
    text-overflow: ellipsis; 
} 

根據需要調整寬度,並自動使用省略號來截斷文本。

+0

Upvoting - 因爲你會有省略號顯示截斷,這可以根據頁面的大小動態調整大小。 – RobertMaysJr

+0

可愛,但效率低下,並有嚴重的瀏覽器支持問題。 – 2012-09-03 21:11:22

+0

受所有主流瀏覽器支持:http://www.w3schools.com/cssref/css3_pr_text-overflow.asp – RobertMaysJr