Jump to content

limiting string length


mattennant

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
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.