1

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;
        }
    }
urfusion
  • 5,528
  • 5
  • 50
  • 87
EBuzila
  • 211
  • 2
  • 4
  • 16
  • i've tried this http://stackoverflow.com/questions/25658187/limit-text-words-without-cut-last-word but i don't have a searched word. My output is pulled from a database. – EBuzila Mar 02 '16 at 09:55
  • 2
    Can you add any example? – Sougata Bose Mar 02 '16 at 09:55
  • Let's say that a title is too long (longer than 27chars.). I want it to be displayed as "This is a long title and i make an excerpt but last ...", so last word to be full, then add '...'. Hope you get it. – EBuzila Mar 02 '16 at 09:57
  • What about to use CSS `text-overflow: ellipsis;`? http://www.w3schools.com/cssref/css3_pr_text-overflow.asp – mitkosoft Mar 02 '16 at 09:57
  • 1
    Have you tried this? http://stackoverflow.com/questions/1104306/how-to-get-first-x-chars-from-a-string-without-cutting-off-the-last-word – Klian Mar 02 '16 at 09:58
  • What about the last word? You want to remove words between? – Sougata Bose Mar 02 '16 at 09:59
  • mitkosoft cool one but still cut the last word. Sougata, i want the excerpt, only the excerpt, but with full words. When i say last word, it's refer to the excerpt's last word. – EBuzila Mar 02 '16 at 10:08
  • Finally! Thanx Klian, one of the solution from your link worked! How can i vote your answer? – EBuzila Mar 02 '16 at 10:25

4 Answers4

4

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;
}
EBuzila
  • 211
  • 2
  • 4
  • 16
3
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;
}
Mohd Zubair Khan
  • 263
  • 2
  • 16
3

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;
}
Amarjit Singh
  • 2,068
  • 19
  • 52
2

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...
gamesmad
  • 399
  • 1
  • 2
  • 14