Error with my Limit String Length code...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sampage
Forum Newbie
Posts: 22
Joined: Sat Mar 18, 2006 6:17 pm

Error with my Limit String Length code...

Post 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??
User avatar
tasteslikepurple
Forum Commoner
Posts: 46
Joined: Thu Jan 26, 2006 3:38 am
Location: Bath, UK

Post 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);
?>
Post Reply