将变量限制为仅显示50个字符


Limiting a variable to display only 50 characters

如何将下面的锚文本限制为仅显示50个字符?

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

substr

或者,您可以使用CSS:

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

根据需要调整宽度,省略号将自动用于剪切文本。

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

您可以使用substr函数,如

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