<?php
$string = "Oh squiggly line in my eye fluid. I see you lurking there on the peripheral of my vision. But when I try to look at you, you scurry away. Are you shy, squiggly line? Why only when I ignore you, do you return to the center of my eye? Oh, squiggly line, it's alright, you are forgiven.";
echo wordlimit($string, 20);
function wordlimit($string, $limit) {
$overflow = true;
$array = explode(" ", $string);
$output = '';
for ($i = 0; $i < $limit; $i++) {
if (isset($array[$i])) {
$output .= $array[$i] . " ";
}
else {
$overflow = false;
}
}
return trim($output) . ($overflow === true ? "..." : '');
}
?>