1

我希望描述、分辨率和附加注释在超过 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 here
解析:php here
附加说明:php here
以此类推...

4

2 回答 2

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;

希望有帮助。

于 2012-07-18T14:34:55.050 回答
0

为什么不使用 PHP 函数?

function cutLongWord($String='', $Length=20, $Suffix='...'){
    return (strlen($String) > $Length ? substr($String, 0, $Length).$Suffix : $String);
}   
于 2012-07-18T14:19:04.173 回答