miércoles, 10 de marzo de 2010

Display a specified number of characters in a field in PHP

If you want to show X number of characters, for example, a field in a form, this piece of code could help you.

if (strlen($campo)>20)
$cadena=substr($campo,0,20)."...";
else
$cadena=$campo;

echo $cadena;

So if you have more than 20 characters in this case, it will cut the string and will add ellipsis. If not, it shows the whole chain.

Greetings everybody!

The Programmer's Reemerge

Hi everybody!

From now on I will direct my blog to the programming part, writing down here everything has helped me during my times as a programmer or in my job. Here you'll be able to find how to solve some problems in java, php, joomla ...

I hope you find it useful!