Page 1 of 1

Error with my Limit String Length code...

Posted: Tue Apr 11, 2006 1:50 pm
by sampage
I can't seem to see what is wrong with this code:

Code: Select all

<?  // Limit Breif to 10 characters in User Profile

$new_brief = $user_brief;
$max_length = 10;
function truncate_string($new_brief, $max_length){

if (strlen($new_brief) > $max_length) {
$new_brief = substr($new_brief,$max_length);
$new_brief .= '...';
}
return $new_brief;
}
						
echo nl2br($new_brief);
?>
It always prints the full string rather than the cut down version??

Posted: Tue Apr 11, 2006 2:09 pm
by tasteslikepurple
you actually need to call the function too :)

Code: Select all

<?  // Limit Breif to 10 characters in User Profile
$max_length = 10;
$new_brief = truncate_string ($user_brief, $max_length);

function truncate_string($new_brief, $max_length){
  if (strlen($new_brief) > $max_length) {
    $new_brief = substr($new_brief,0 , $max_length);
    $new_brief .= '...';
  }
  return $new_brief;
}
echo nl2br($new_brief);
?>