word wrap - How to display "..." (3 dots) if a sentence takes more than 2 lines in php? -
i looked on online not find. found use string longer specific number defined need display dots if sentence takes more 2 lines.
for example, sentence below long :
the eiffel tower wrought iron lattice tower on champ de mars in paris, france. named
after engineer gustave eiffel, company designed , built tower. tower 324
metres (1,063 ft) tall, same height 81-storey building, , tallest structure in
paris. base square, measuring 125 metres (410 ft) on each side.
i display above sentence as:
the eiffel tower wrought iron lattice tower on champ de mars in paris, france. named after engineer gustave eiffel, company designed , built tower. tower is...
try below function
<?php function trim_text($text, $count){ $text = str_replace(" ", " ", $text); $string = explode(" ", $text); ( $wordcounter = 0; $wordcounter <= $count;wordcounter++ ){ $trimed .= $string[$wordcounter]; if ( $wordcounter < $count ){ $trimed .= " "; } else { $trimed .= "..."; } } $trimed = trim($trimed); return $trimed; } ?>
usage
<?php $string = "one 2 3 four"; echo trim_text($string, 3); ?>
result:
one 2 three...
Comments
Post a Comment