2010-12-27 6 views
0

데이터를 테이블 셀에 추가하는 PHP 루프가 있습니다. 그러나 테이블 셀에 정적 크기를 적용하여 셀 내부에 들어갈 수있는 것보다 많은 데이터가 반환되는 경우 초과 문자를 잘라내어 "..."로 끝내길 원합니다.테이블 셀에 표시되는 문자 수 제한

예를 들어 한 데이터 항목은 270 자이지만 첫 번째 항목 만 표 셀에 표시됩니다. 따라가 "..."

어떻게하는 방법에 대한 아이디어?

감사합니다!

답변

8
if (strlen($str) > 100) $str = substr($str, 0, 100) . "..."; 
+0

멋진 원 라이너 +1 –

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

    echo $message; 
} 

print_dots($long_text); 
0
$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";