PHP function to cut string into a specific length

PHP function to cut string into a specific length. Very useful for "Read More" links.

###php###
function wCut($str, $maxLength = 40) {
 $strToCount = html_entity_decode($str);
 if (strlen($strToCount) <= $maxLength) {
  return $str;
 }
 $s2 = substr($strToCount, 0, $maxLength - 3);
 $s2 .= "...";
 return htmlentities($s2);
}

$str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla nunc risus, euismod ac, facilisis vitae, malesuada in, urna. Sed ut est.';

echo wCut($str); // Lorem ipsum dolor sit amet, consectet...
###/php###

Change "###php###" and "###/php###" accordingly.

Comments

Popular posts from this blog

The Importance Outdoors Activities For Children (cont.)

Control your heart and your feelings...