1

I want description, resolution and additional_notes to get cut off when it gets longer than 6 words and then have the "..." with a link to "see full case". I only want this to show on the fields that got cut off, not the ones that are less than 6 words because the full field would already be showing. If you guys could please help me figure out how to do this it would make my day! thank you!

I have this query which so far only works to get the field to cut off at 6 words:

$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}%'");

The results show up in a table sort of like this:

Description: php here
Resolution: php here
Additional Notes: php here
and so on...

Alexandra
  • 65
  • 1
  • 2
  • 13

2 Answers2

1

How about this:

<?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 );

?>

Output:

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

Or a CSS way:

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

Hope that helps.

Peon
  • 7,902
  • 7
  • 59
  • 100
0

Why not using a PHP function ?

function cutLongWord($String='', $Length=20, $Suffix='...'){
    return (strlen($String) > $Length ? substr($String, 0, $Length).$Suffix : $String);
}   
David Bélanger
  • 7,400
  • 4
  • 37
  • 55
  • Would this be in addition to the query I have already? or removing the SUBSTRING_INDEXs from my query and using this, sorry I'm very new at this. – Alexandra Jul 18 '12 at 14:24
  • @AlexandraMolina You could pull the column without doing a substring function inside the query, then use this function on PHP's level to cut the word. But this function allow you to cut, not by word, but by the length you want. – David Bélanger Jul 18 '12 at 14:27
  • Thanks, but my biggest concern is to not cut off a word, instead have it end and then add the link, the table could extend into another row, that's not the problem, I just don't want a huge paragraph. – Alexandra Jul 18 '12 at 14:31
  • @AlexandraMolina Ah ok. At least someone maybe will use this function ;) – David Bélanger Jul 18 '12 at 15:27