Jump to content

Archived

This topic is now archived and is closed to further replies.

mattennant

limiting string length

Recommended Posts

hello there

i'm trying to achieve that ...[read more]

type of thing

i'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 appreciated

mat

Share this post


Link to post
Share on other sites
[!--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 there

i'm trying to achieve that ...[read more]

type of thing

i'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

any help appreciated

mat
[/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

Share this post


Link to post
Share on other sites
your echo statement is governed by your if statement. You should move the echo after the curly bracket which closes the if statement.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.