Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 限制表格单元格中显示的字符数_Php_Html - Fatal编程技术网

Php 限制表格单元格中显示的字符数

Php 限制表格单元格中显示的字符数,php,html,Php,Html,我有一个PHP循环,它将数据添加到表单元格中。但是,我希望对表单元格应用静态大小,因此,如果返回的数据超出了单元格内的容量,我希望将多余的字符切掉,并以“…”结尾 例如,一个数据条目有270个字符,但表格单元格中仅显示前100个字符。后面跟着一个“…” 有什么办法吗 谢谢 $table_cell_data=“”;//这将在单元格中保存数据 if (strlen($str) > 100) $str = substr($str, 0, 100) . "..."; $cell_limit=10

我有一个PHP循环,它将数据添加到表单元格中。但是,我希望对表单元格应用静态大小,因此,如果返回的数据超出了单元格内的容量,我希望将多余的字符切掉,并以“…”结尾

例如,一个数据条目有270个字符,但表格单元格中仅显示前100个字符。后面跟着一个“…”

有什么办法吗

谢谢

$table_cell_data=“”;//这将在单元格中保存数据
if (strlen($str) > 100) $str = substr($str, 0, 100) . "...";
$cell_limit=100;//这将是您想要的字符数限制 //检查表格单元格数据是否大于限制 if(strlen($table\u cell\u data)>$cell\u limit){ //这是为了将字符限制保持在100而不是103。可选 $sub_string=$cell_limit-3; //获取子字符串并附加。。。 $table_cell_data=substr($table_cell_data,0,$sub_string)。“…”; } //测试输出 echo$table\u cell\u数据。“
\n”;
您可以使用

printf(“%s”,mb_strimwidth($cellContent,01100,…);
如果要根据单词边界截断,请参见

您还可以使用CSS属性
文本溢出:省略号

不幸的是,浏览器支持各不相同

function print_dots($message, $length = 100) {
  if(strlen($message) >= $length + 3) {
    $message = substr($message, 0, $length) . '...';
  }

  echo $message;
}

print_dots($long_text);
$table_cell_data = "";  // This would hold the data in the cell
$cell_limit      = 100; // This would be the limit of characters you wanted

// Check if table cell data is greater than the limit
if(strlen($table_cell_data) > $cell_limit) {
   // this is to keep the character limit to 100 instead of 103. OPTIONAL
   $sub_string = $cell_limit - 3; 

   // Take the sub string and append the ...
   $table_cell_data = substr($table_cell_data,0,$sub_string)."...";
}

// Testing output
echo $table_cell_data."<br />\n";
printf('<td>%s</td>', mb_strimwidth($cellContent, 0, 100, '…'));