Search:
Main Menu
Login | RSS |

Quick PHP Code Tips and Examples

PHP Programming Tips, Tutorials and Source Code Examples for newbie

Truncate long text with PHP (excerpt function)

May 4th, 2007 by Jon Moffet

Truncate is a handy function which can truncate a long text into specific length string and adds elipsis (...) after it.

This is an example of how the function "truncate($text,47)" works :
Before

CODE:
  1. This is a very long text that I type as an example to show you the functionality of truncate

After

CODE:
  1. This is a very long text that I type as an exam....

Here's the PHP code that can let you do just that :

PHP:
  1. function truncate ($str, $length=10, $trailing='...')
  2. {
  3. /*
  4. ** $str -String to truncate
  5. ** $length - length to truncate
  6. ** $trailing - the trailing character, default: "..."
  7. */
  8.       // take off chars for the trailing
  9.       $length-=mb_strlen($trailing);
  10.       if (mb_strlen($str)> $length)
  11.       {
  12.          // string exceeded length, truncate and add trailing dots
  13.          return mb_substr($str,0,$length).$trailing;
  14.       }
  15.       else
  16.       {
  17.          // string was already short enough, return the string
  18.          $res = $str;
  19.       }
  20.  
  21.       return $res;
  22.  
  23. }

Truncate as an excerpt function
Truncate can be use to create excerpts from a very long text to serve as a preview or summary on your PHP application.

Tags: , , , , , ,

Bookmark Post:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • NewsVine
  • Reddit
  • Netvouz
  • Spurl
  • Furl
  • digg
  • YahooMyWeb
  • del.icio.us

Posted in Uncategorized |

Related Posts

9 Responses

  1. how to truncate text in php - Dogpile Web Search Says:

    Kramer auto Pingback[...] http://www.the-art-of-web.com/php/truncate/ • Found on Google, Windows Live, Yahoo! Search Truncate long text with PHP (excerpt function) » Quick PHP Code … Truncate as an excerpt function Truncate can be use to create excerpts from a very long text to [...]

  2. Vasi Says:

    What if i want the text truncated at first ’space’ character after the number i give it, so that i evoid words being cut. Have you got a solution for this? Thanks

  3. arslion Says:

    http://news-world.us/pics

  4. Michael Says:

    If there’s html in the text, things could go south very quickly.

  5. Helen Hunt Says:

    Thanks for this tip..
    I spent the whole day thinking of how to implement this functionality and was lucky to stumble on your tip.

    Thanks for sharing :)

  6. Tecnología Diaria Says:

    links from Technoratide la función. Como ven, esta función es ideal para mostrar un resumen en PHP, es más, la usé para un proyecto en la universidad. Cualquier duda u optimización, observación o recomendación, es bienvenida Via:PHP CodeParticipamos en el concurso 20 Blogs Peruanos, Categoría: Tecnología y Ciencia. Apóyanos con tu voto. La votación te tomará menos de 1 minuto. Muchas gracias.            

  7. Wordpress yazý sýnýrlamasý - CMSTURK.NET ÝYS (CMS) Forumlarý Says:

    Kramer auto Pingback[...] yazý sýnýrlamasý css deðl php’de exerpt fonksiyonunu wordpress’e uyarlamak gerekiyor… Þurada bir yazý var php ile uðraþarak yapabilirsiniz,ayrýca bazý temalardada kullanýlmýþ,bulabilisiniz, [...]

  8. Tekst korter maken - SiteDeals Says:

    Kramer auto Pingback[...] http://phpcode.mypapit.net/truncate-…p-function/33/ [...]

  9. Php function limiting the ammout of passed in characters. - Stack Overflow Says:

    Kramer auto Pingback[...] got it from here [...]

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.