Posted By


LondonWeb on 08/20/08

Tagged


Statistics


Viewed 91 times
Favorited by 0 user(s)

Related snippets


CortaString - CutString


/ Published in: PHP
Save to your folder(s)

Para não cortar a palavra ao meio.
Its for not to cut a word in the middle.
//Ex:
Então, chapeuzinho vermelho decide tomar no c... (feio, nao?)
Então, chapeuzinho vermelho decide tomar no cantinho...


Copy this code and paste it in your HTML
  1. define("TRUNC_BEFORE_LENGHT", 0);
  2. define("TRUNC_AFTER_LENGHT", 1);
  3. function str_truncate($str, $length, $rep=TRUNC_BEFORE_LENGHT){
  4. //adicionada em 27/06/2006 para corrigir um bug
  5. if(strlen($str)<=$length) return $str;
  6. if($rep == TRUNC_BEFORE_LENGHT) $oc = strrpos(substr($str,0,$length),' ');
  7. if($rep == TRUNC_AFTER_LENGHT) $oc = strpos(substr($str,$length),' ') + $length;
  8. return substr($str, 0, $oc);
  9. }
  10.  
  11.  
  12. <?
  13. $frase = "Então, chapeuzinho vermelho decide tomar no cantinho bem escondida, uma lata de leite condensado.";
  14. print str_truncate($frase, 45, TRUNC_AFTER_LENGHT) . '...';
  15. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.