Skip to content

Instantly share code, notes, and snippets.

@MilkZoft
Created January 20, 2012 03:51
Show Gist options
  • Save MilkZoft/1644930 to your computer and use it in GitHub Desktop.
Save MilkZoft/1644930 to your computer and use it in GitHub Desktop.
codejobs - Cut words - PHP
<?php
function cut($text, $length = 12, $type = "text", $file = FALSE, $elipsis = FALSE) {
if($type === "text") {
$elipsis = "...";
$words = explode(" ", $text);
if(count($words) > $length) {
return str_replace("\n", "", implode(" ", array_slice($words, 0, $length)) . $elipsis);
}
return $text;
} elseif($type === "word") {
if($file) {
if(strlen($text) < $length) {
$max = strlen($text);
}
return substr($text, 0, $length);
} else {
if(strlen($text) < 13) {
return $text;
}
if(!$elipsis) {
return substr($text, 0, $length);
} else {
return substr($text, 0, $length) . $elipsis;
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment