Php cortar string respetando palabras beta 0.2


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

echo cortarAdelante("hola mundo", 3, 1, 1 ); #Return hola


Copy this code and paste it in your HTML
  1. function cortarAdelante( $cadena, $cuantos, $ponerPuntos=0, $sacarTags=0, $puntos1 = '...' ){
  2. $puntos = '';
  3. if( $ponerPuntos == 1 ){
  4. if( strlen( $cadena ) >= $cuantos) {
  5. $puntos = $puntos1;
  6. }
  7. }
  8. if( $sacarTags==1 ){
  9. $cadeba = sacarTags( $cadena );
  10. }
  11. $cadena .= ' ';
  12. $cadena = substr( $cadena, 0, $cuantos + 1 );
  13. $cadena = substr( $cadena, 0, strrpos( $cadena,' ') );
  14. return $cadena.$puntos;
  15. }
  16.  
  17. #funciones adicionales que usara
  18. function sacarTags( $str ){
  19. if( trim( $str )!='' ){
  20. $str = ereg_replace( "<([^>]+)>", "", $str );
  21. return $str;
  22. }else{
  23. return $str;
  24. }
  25. }
  26.  
  27. function sacarHref( $str ){
  28. if( trim( $str ) != '' ){
  29. $str = eregi_replace( "<a([^>]+)>|</a>", "", $str );
  30. return $str;
  31. }else{
  32. return $str;
  33. }
  34. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.