Acortar texto con palabras completas


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

Con esta funcion podemos recortar una cadena de texto indicandole cuantos caracteres queremos que tenga y ademas no cortara la ultima palabra.


Copy this code and paste it in your HTML
  1. function acortar($cadena, $limite, $corte=" ", $pad="...") {
  2. if(strlen($cadena) <= $limite)
  3. return $cadena;
  4. if(false !== ($breakpoint = strpos($cadena, $corte, $limite))) {
  5. if($breakpoint < strlen($cadena) - 1) {
  6. $cadena = substr($cadena, 0, $breakpoint) . $pad;
  7. }
  8. }
  9. return $cadena;
  10. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.