EBuzila
EBuzila

Reputation: 229

Make an excerpt with php without cutt last word

Hy! I'm trying to make an excerpt for long titles but last word should not be cutted. I've read an related topic of this question but it seems it refer to another problem (on search result). here's my function now

function excerpt($title) {
        $new = substr($title, 0, 27);

        if (strlen($title) > 30) {
            return $new.'...';
        } else {
            return $title;
        }
    }

Upvotes: 3

Views: 12724

Answers (4)

Amarjit Singh
Amarjit Singh

Reputation: 2154

I have modified @mohd zubair khan's solution to meet the requirements of the question.

the question was about shortening of string without cutting it from tails. So here is my code.

function strWordCut($string,$length)
{
    $str_len = strlen($string);
    $string = strip_tags($string);

    if ($str_len > $length) {

        // truncate string
        $stringCut = substr($string, 0, $length-15);
        $string = $stringCut.'.....'.substr($string, $str_len-10, $str_len-1);
    }
    return $string;
}

Upvotes: 3

Mohd Zubair Khan
Mohd Zubair Khan

Reputation: 272

function strWordCut($string,$length,$end='....')
{
    $string = strip_tags($string);

    if (strlen($string) > $length) {

        // truncate string
        $stringCut = substr($string, 0, $length);

        // make sure it ends in a word so assassinate doesn't become ass...
        $string = substr($stringCut, 0, strrpos($stringCut, ' ')).$end;
    }
    return $string;
}

Upvotes: 4

EBuzila
EBuzila

Reputation: 229

I was actually able to solve it with:

if (strlen($title) < 30) {
     return $title;
} else {

   $new = wordwrap($title, 28);
   $new = explode("\n", $new);

   $new = $new[0] . '...';

   return $new;
}

Upvotes: 3

gamesmad
gamesmad

Reputation: 409

Here is a function that does what you want, and a test function to check it is working as expected.

function excerpt($title, $cutOffLength) {

    $charAtPosition = "";
    $titleLength = strlen($title);

    do {
        $cutOffLength++;
        $charAtPosition = substr($title, $cutOffLength, 1);
    } while ($cutOffLength < $titleLength && $charAtPosition != " ");

    return substr($title, 0, $cutOffLength) . '...';

}

function test_excerpt($length) {

    echo excerpt("This is a very long sentence that i would like to be shortened", $length);
    echo "
";
    echo excerpt("The quick brown fox jumps over the lazy dog", $length);
    echo "
";
    echo excerpt("nospace", $length);
    echo "
";
    echo excerpt("A short", $length);
    echo "

";

}

test_excerpt(5);
test_excerpt(10);
test_excerpt(15);
test_excerpt(20);
test_excerpt(50);

Outputs -

This is...
The quick...
nospace...
A short...

This is a very...
The quick brown...
nospace...
A short...

This is a very long...
The quick brown fox...
nospace...
A short...

This is a very long sentence...
The quick brown fox jumps...
nospace...
A short...

This is a very long sentence that i would like to be...
The quick brown fox jumps over the lazy dog...
nospace...
A short...

Upvotes: 2

Related Questions