mattennant Posted June 7, 2006 Share Posted June 7, 2006 hello therei'm trying to achieve that ...[read more]type of thingi've got a little bit of code that limits the length of a string from a column of my database. It works fine unless the string length is less than the limit i have assigned. in that instance nothing is displayed.I'm sure it must be a simple thing i'm missing, but i'm struggling the code i'm using goes like this[code]<? $var = $row_projects['description'];$varlength = strlen($var); $limit = 300;if ($varlength > $limit) { $var = substr($var,0,$limit); echo $var;} ?>[/code]any help appreciatedmat Quote Link to comment https://forums.phpfreaks.com/topic/11403-limiting-string-length/ Share on other sites More sharing options...
obsidian Posted June 7, 2006 Share Posted June 7, 2006 [!--quoteo(post=380965:date=Jun 7 2006, 08:21 AM:name=mattennant)--][div class=\'quotetop\']QUOTE(mattennant @ Jun 7 2006, 08:21 AM) [snapback]380965[/snapback][/div][div class=\'quotemain\'][!--quotec--]hello therei'm trying to achieve that ...[read more]type of thingi've got a little bit of code that limits the length of a string from a column of my database. It works fine unless the string length is less than the limit i have assigned. in that instance nothing is displayed.I'm sure it must be a simple thing i'm missing, but i'm struggling the code i'm using goes like thisany help appreciatedmat[/quote]try something like this:[code]function truncateMe($String, $limit = 300) { $len = strlen($String); if ($len > $limit) $String = substr($String, 0, $limit) . '...'; return $String;}echo truncateMe($row_projects['description']);[/code]hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/11403-limiting-string-length/#findComment-42769 Share on other sites More sharing options...
justsomeone Posted June 7, 2006 Share Posted June 7, 2006 your echo statement is governed by your if statement. You should move the echo after the curly bracket which closes the if statement. Quote Link to comment https://forums.phpfreaks.com/topic/11403-limiting-string-length/#findComment-42774 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.