Jump to content

Reduce Displayed Text Length


Dysan

Recommended Posts

Hi,

 

I am currently displaying people's names on the screen, but some people's names are longer than others. How do I display only 6 characters of a person's name, then display three dots (...) at the end.

 

$result = mysql_query("SELECT * FROM person");

while($row = mysql_fetch_array($result))
{
  echo $row['name'];
}

Link to comment
https://forums.phpfreaks.com/topic/79538-reduce-displayed-text-length/
Share on other sites

try this

<?php
echo (strlen($row['name'])>6)?substr($row['name'], 0, 6)."...":$row['name'];
?>

 

 

OR a function (if used many times)

 

<?php
echo shortname($row['name'], 6);

function shortname($name, $len)
{
return (strlen($name)>$len)?substr($name, 0, $len)."...":$name;
}
?>

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.