rveitch
5/25/2016 - 7:30 PM

Limit String Characters, trim trailing whitespace and add ellipses to the end.

Limit String Characters, trim trailing whitespace and add ellipses to the end.

<?php
$string = 'Jamie Dietman, Brainerd Lakes area guide gives his thoughts on muskie stocking across the region and new Red Lake winter regulations. And Brian Peterson gives us a glimpse into the world of fat bikes and just where you can take them when the northland is covered in snow.';

echo 'Original String Length: ' . strlen($string);
echo '<br>';
echo substr($string, 0, 255);

echo '<br><br>';

$new_string = substr_replace(rtrim(substr($string, 0, 252), " "), '...', 252);
echo 'Truncated String Length: ' . strlen($new_string);
echo '<br>';
echo $new_string;

/* Output:
Original String Length: 271
Jamie Dietman, Brainerd Lakes area guide gives his thoughts on muskie stocking across the region and new Red Lake winter regulations. And Brian Peterson gives us a glimpse into the world of fat bikes and just where you can take them when the northland is 

Truncated String Length: 254
Jamie Dietman, Brainerd Lakes area guide gives his thoughts on muskie stocking across the region and new Red Lake winter regulations. And Brian Peterson gives us a glimpse into the world of fat bikes and just where you can take them when the northland...
*/