I am currently developing a CMS and want to add in the functionality of limiting an excerpt of text by its line length. Say I want to control the display of text and ensure all blocks are the same height by controlling the amount of lines of text that show.
Does any know of any pre-written function or perhaps article to help get me started? It would gratefully be appreciated. Thank you.
You can use the native wordwrap() function to control how many characters are permitted per line. This function would also permit you to limit the number of lines you would like to show as well by exploding the results on the break-sequence, and gluing together any number of the resulting array values.
$s = "This is the long string I wish to wrap to multiple lines.";
list( $one, $two ) = explode( PHP_EOL, wordwrap( $s, 25, PHP_EOL ) );
# This is the long string I wish to wrap to multiple...
echo "$one $two...";
Here I have limited each line to 25 characters, at most. I've then taken the first two lines and printed them out, one after another.
Demo: http://codepad.org/8aCusZGQ
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With