Been on the Hawkee.com Snippet page and wondered how to get a short description?
(Example Image.)
Well now you can :)
Enjoy :)
<?php
function truncate($string, $del) {
$len = strlen($string);
if ($len > $del) {
$new = substr($string,0,$del)."...";
return $new;
}
else return $string;
}
$arg = "Hello World. How are you?";
echo $arg."n"; // Returns "Hello World. How are you?"
echo truncate($arg, 5)."n"; // Returns "Hello..."
echo truncate($arg, 7)."n"; // Returns "Hello W..."
?>
Here's what i use. It doesn't leave any chopped word on the end.
Very handy for long strings..
<?php
function do_TrimString($string, $MaxSTRLEN = null) {
if ($MaxSTRLEN == null) { global $MaxSTRLEN; }
if (strlen($string) > $MaxSTRLEN) {
$string = substr($string, 0, $MaxSTRLEN);
$end = strrpos($string, ' ');
if ($end === false) {
$end = $MaxSTRLEN;
}
$string = substr($string, 0, $end) . '...';
}
return $string;
}
?>