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
https://forums.phpfreaks.com/topic/11403-limiting-string-length/
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
https://forums.phpfreaks.com/topic/11403-limiting-string-length/#findComment-42769
Share on other sites

Archived

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

×
×
  • 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.