-1

I looked all over online for this but could not find. I only found for those that use string longer than a specific number defined but I need to display those dots if the sentence takes more than 2 lines.

For example, sentence below is long :

The Eiffel Tower is a wrought iron lattice tower on the Champ de Mars in Paris, France. It is named
after the engineer Gustave Eiffel, whose company designed and built the tower. The tower is 324
metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in
Paris. Its base is square, measuring 125 metres (410 ft) on each side.

I would like to display the above sentence as:

The Eiffel Tower is a wrought iron lattice tower on the Champ de Mars in Paris, France. It is named after the engineer Gustave Eiffel, whose company designed and built the tower. The tower is...

  • In my device your two lines sentence is a four line one. Besides that, if you already has the function to cut off a sentence if it is bigger than x characters, it's just the case of appending the three dots at the end of the ripped string. Where is the problem? – statosdotcom Jul 15 '16 at 23:06
  • 1
    Surely more of a javascript deal here. How can you possibly know the view-port width? – Jonathan Jul 15 '16 at 23:47
  • How do you decide what a line is? By width? New line characters? If the width is not fixed, you would be better off using CSS: text-overflow: ellipsis; – WhoIsRich Jul 15 '16 at 23:59
  • 2 line css ellipsis is quite difficult to achieve afair – Jonathan Jul 16 '16 at 00:26

1 Answers1

-2

Try below the function

    <?php
    function trim_text($text, $count){
        $text = str_replace("  ", " ", $text);
        $string = explode(" ", $text);
        for ( $wordCounter = 0; $wordCounter <= $count;wordCounter++ ){
            $trimed .= $string[$wordCounter];
            if ( $wordCounter < $count ){
                $trimed .= " ";
            }
            else {
                $trimed .= "...";
            }
        }
    $trimed = trim($trimed);
    return $trimed;
    }
?>

Usage

<?php
    $string = "one two three four";
    echo trim_text($string, 3);
?> 

Result:

one two three...

Chandru
  • 133
  • 10