Am试图限制此代码的输出<tr><td valign='"middle'" style='"font-size:12px; padding-left:10px; padding-bottom:5px; height:10px'">".$postDetails['big_title']."</td></tr>
那么,在UTF8中,它将从这个"废话废话"变成100个字符的"废话废话…"?
使用函数substr
最多保留字符串中的100个字符。示例:
<tr><td valign='"middle'" style='"font-size:12px; padding-left:10px; padding-bottom:5px; height:10px'">".substr($postDetails['big_title'], 0, 100)."</td></tr>
您可以使用
substr(strip_tags($postDetails['big_title']), 0, 70);
它将去掉输出中的任何附加元素。
只需检查字符串是否超过100个字符,如果是,则使用substr
并附加省略号,如果不是,则保持原样。示例:
$big_title = (strlen($postDetails['big_title']) > 100) ? substr($postDetails['big_title'], 0, 100) . '…' : $postDetails['big_title'];
echo "
<tr>
<td
valign='"middle'"
style='"font-size:12px; padding-left:10px; padding-bottom:5px; height:10px'"
>"
.$big_title.
"</td>
</tr>";
询问者表示他想要省略号。我认为自定义功能会很好地工作:
function limitOutput($string, $limit){
if (strlen($string) > $limit){
$string = substr($string, 0, $limit - 3) . '...';
}
return $string;
}
这可以这样使用:
<tr><td valign='"middle'" style='"font-size:12px; padding-left:10px; padding-bottom:5px;height:10px'">".limitOutput($postDetails['big_title'],100)."</td></tr>
编辑-对其不起作用的响应:
<?php
function limitOutput($string, $limit){
if (strlen($string) > $limit){
$string = substr($string, 0, $limit - 3) . '...';
}
return $string;
}
$postDetails['big_title'] = str_repeat('12345678901234567890', 20);
echo $postDetails['big_title'].'<br><br>';
echo "<tr><td valign='"middle'" style='"font-size:12px; padding-left:10px; padding-bottom:5px;height:10px'">".limitOutput($postDetails['big_title'],100)."</td></tr>";
<tr><td valign='"middle'" style='"font-size:12px; padding-left:10px; padding-bottom:5px; height:10px'">".substr($postDetails['big_title'], 0, 100)."</td></tr>
添加this,0,100,'utf-8')。"…