Jump to content


Photo

limiting string length


  • Please log in to reply
2 replies to this topic

#1 mattennant

mattennant
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 07 June 2006 - 12:21 PM

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

<? $var = $row_projects['description'];

$varlength = strlen($var); 

$limit = 300;

if ($varlength > $limit) { 

$var = substr($var,0,$limit); 

echo $var;

} 
 ?>

any help appreciated

mat


#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 07 June 2006 - 12:29 PM

[!--quoteo(post=380965:date=Jun 7 2006, 08:21 AM:name=mattennant)--][div class=\'quotetop\']QUOTE(mattennant @ Jun 7 2006, 08:21 AM) View Post[/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:
function truncateMe($String, $limit = 300) {
  $len = strlen($String);
  if ($len > $limit) $String = substr($String, 0, $limit) . '...';
  return $String;
}

echo truncateMe($row_projects['description']);

hope that helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 justsomeone

justsomeone
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 07 June 2006 - 12:40 PM

your echo statement is governed by your if statement. You should move the echo after the curly bracket which closes the if statement.
** PHP Gun for Hire **




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users