Skip to content

Instantly share code, notes, and snippets.

@magickatt
Created January 20, 2014 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magickatt/8524328 to your computer and use it in GitHub Desktop.
Save magickatt/8524328 to your computer and use it in GitHub Desktop.
Truncate a string to whole words for a given string length
<?php
$model = "Big ultra super car with mega turbo charger";
$limit = 10;
$words = explode(' ', $model);
$sentence = '';
foreach ($words as $word) {
$sentence .= $word . ' ';
if (strlen($sentence) >= $limit) {
break;
}
}
$sentence .= '...';
print($sentence . "\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment