Created
January 20, 2014 17:14
-
-
Save magickatt/8524328 to your computer and use it in GitHub Desktop.
Truncate a string to whole words for a given string length
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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