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!

No hay comentarios:

Publicar un comentario