andretanguy Posted March 12, 2007 Share Posted March 12, 2007 Hey there! ok so at the moment i do this: <?php echo $row['short_desc']; ?>... but i want to limit the output of short_desc to 300 chars. i came across this: <?php function truncate_string($string, $max_length){ if (strlen($string) > $max_length) { $string = substr($string,0,$max_length); $string .= '..'; } return $string; }?> i need some help turning $string into my short_desc - if someone can help that'd be awesome cheers Andre Link to comment https://forums.phpfreaks.com/topic/42335-php-limit-characters-in-string/ Share on other sites More sharing options...
paul2463 Posted March 12, 2007 Share Posted March 12, 2007 <?php echo truncate_string($row['short_desc'], 300); ?> will only print out the first 300 characters with an appended .. on the end Link to comment https://forums.phpfreaks.com/topic/42335-php-limit-characters-in-string/#findComment-205375 Share on other sites More sharing options...
andretanguy Posted March 12, 2007 Author Share Posted March 12, 2007 its generating a load of errors - is there another solution? Link to comment https://forums.phpfreaks.com/topic/42335-php-limit-characters-in-string/#findComment-205379 Share on other sites More sharing options...
andretanguy Posted March 12, 2007 Author Share Posted March 12, 2007 it works like this: <?php echo substr($row['short_desc'], 0, 130); ?>... hope it helps someone else cheers Andre Link to comment https://forums.phpfreaks.com/topic/42335-php-limit-characters-in-string/#findComment-205382 Share on other sites More sharing options...
vbnullchar Posted March 12, 2007 Share Posted March 12, 2007 function trunc($details,$max) { if(strlen($details)>$max) { $details = substr($details,0,$max); $i = strrpos($details," "); $details = substr($details,0,$i); $details = $details."..."; } return $details; } echo trunc($row['short_desc']); Link to comment https://forums.phpfreaks.com/topic/42335-php-limit-characters-in-string/#findComment-205384 Share on other sites More sharing options...
paul2463 Posted March 12, 2007 Share Posted March 12, 2007 vbnullchar you forgot to pass it the $max should it be echo trunc($row['short_desc'],300); Link to comment https://forums.phpfreaks.com/topic/42335-php-limit-characters-in-string/#findComment-205388 Share on other sites More sharing options...
vbnullchar Posted March 12, 2007 Share Posted March 12, 2007 oh, Ahehe thanks! Link to comment https://forums.phpfreaks.com/topic/42335-php-limit-characters-in-string/#findComment-205389 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.