Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Partial Display

Status
Not open for further replies.

alsaffar

Programmer
Joined
Oct 25, 2001
Messages
165
Location
KW
Hi everybody,

How can I display only 50 characters of a DB field content?

e.g.

Contents of Comments field:
"I LOVE PHP MORE THAN MY SELF, I LOVE PHP MORE THAN MY SELF, I LOVE PHP MORE THAN MY SELF, I LOVE PHP MORE THAN MY SELF, I LOVE PHP MORE THAN MY SELF, I LOVE PHP MORE THAN MY SELF, I LOVE PHP MORE THAN MY SELF, I LOVE PHP MORE THAN MY SELF"

If the length of the field is less or equal to 50 char I want to display all the content. But if the length of the field is greater than 50 I want to display 47 characters and then one space and to dots " .."

Please guys, I need a working module.
 
$string="DB VALUE HERE";
if(strlen($string)>=50)
{
echo substr(%string,0,50);
}
else
echo $string;

Known is handfull, Unknown is worldfull
 
Code:
$string="DB VALUE HERE";
# 50 or less
if(strlen($string)<=50){
 echo $string;
} else {
 echo substr($string,0,47).'...';
}

This trims longer strings to 47 and adds the ellipsis.

You could compact the code into one line using the ternary operator:
Code:
echo strlen($string <=50)? $string : substr($string,0,47).'...';
 
oops happens from not reading the post correctly...

DRJ is right...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top