我需要添加" ..."在被切断的田地的尽头

时间:2012-07-18 14:15:25

标签: php mysql

我希望描述,分辨率和additional_notes在超过6个单词时被切断,然后让“...”链接到“查看完整的大小写”。我只希望这显示在被切断的字段上,而不是那些少于6个字的字段,因为完整字段已经显示。如果你们可以请帮助我弄清楚如何做到这一点,这将是我的一天!谢谢!

我有这个查询,到目前为止只能使字段切换为6个字:

$sql = ("SELECT id, sso, case_status, assigned_to, updated, created, SUBSTRING_INDEX(additional_notes,' ',6) as additional_notes, SUBSTRING_INDEX(resolution,' ',6) as resolution, SUBSTRING_INDEX(description,' ',6) as description FROM rmstable2 WHERE sso LIKE '%{$sso}%'");

结果显示在表格中:

描述:php这里
解决方案:php这里
附加说明:php这里
等等...

2 个答案:

答案 0 :(得分:1)

这个怎么样:

<?php

$text = array(
    'some long texts with multiple words, more then seven',
    'some long texts with multiple words, more then seven',
    'some long texts with multiple words, more then seven'
);

$new_text = array();
foreach ( $text as $key => $string ) {
    $words = explode( ' ', $string );
    for ( $k=0; $k<6; $k++ ) {
        if ( $words[ $k ] ) $new_text[ $key ] .= $words[ $k ] . ' ';
    }
    $new_text[ $key ] .= '...';
    # Or like this, if you don't need the space
    //$new_text[ $key ] = rtrim( $new_text[ $key ] ) . '...';

}
print_r( $new_text );

?>

输出:

Array
(
    [0] => some long texts with multiple words, ...
    [1] => some long texts with multiple words, ...
    [2] => some long texts with multiple words, ...
)

或CSS方式:

overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
width: 200px;

希望有所帮助。

答案 1 :(得分:0)

为什么不使用PHP函数?

function cutLongWord($String='', $Length=20, $Suffix='...'){
    return (strlen($String) > $Length ? substr($String, 0, $Length).$Suffix : $String);
}