Very short snippet: PHP word cut

Sunday, September 21, 2008

Delimits a portion of text to the number of "words":

PHP:
  1. / **
  2. * String word cut
  3. *
  4. * @ Private
  5. * /
  6. $c , $l ) { wordCut function ($ c, $ l) (
  7. explode ( ' ' , $c ) ; $ c = explode ( '', $ c);
  8. $i = 0 ; $i < $l ; $i ++ ) $r [ $i ] = $c [ $i ] ; for ($ i = 0; $ i <$ l, $ i + +) $ r [$ i] = $ c [$ i];
  9. implode ( ' ' , $r ) . '...' ; $ r = implode ( '', $ r).'...';
  10. ; return $ r;
  11. )

Related Post

Was this article helpful?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

9 comments to "Very short snippet: PHP word cut"

  1. getAvatar 1.0
    September 21, 2008 Tom:

    From 2 rows for a esempiuccio you could ask. :)

  2. getAvatar 1.0
    September 21, 2008 Napolux:

    My tagliaStringa ($ string, $ numParole) is a bit 'different ... Not use two arrays, but builds the string by inserting the words as a variable, then cutting off the last space and adding three dots ...

    What?

  3. getAvatar 1.0
    September 22, 2008 Giulio

    Thank you! Carpita and used ...

  4. getAvatar 1.0
  5. getAvatar 1.0
    September 22, 2008 Giovambattista Fazioli:

    @ Tom: Oh, an example for a function "cut the strings" is a bit 'too ... :) However, for example, you can use to:

    PHP:
    1. wordCut ( 'Stringa di esempio abbastanza lunga per essere tagliata' , 5 ) ; $ res = wordCut ( 'sample string long enough to be cut', 5);

    'll Get in $res

      Sample string long enough ... 
  6. getAvatar 1.0
    September 22, 2008 Giovambattista Fazioli:

    @ Napolux: I searched in vain for your role on your blog ... cmq even so would be fine:

    PHP:
    1. $c , $l ) { wordCut function ($ c, $ l) (
    2. explode ( ' ' , $c ) ; $ c = explode ( '', $ c);
    3. $i = 0 ; $i < $l ; $i ++ ) $r [ $i ] = ( $i == 0 ) ? $c [ $i ] : ( ' ' . $c [ $i ] ) ; for ($ i = 0; $ i <$ l, $ i + +) $ r [$ i] = ($ i == 0)? $ c [$ i]: ( ''. $ c [$ i] )
    4. $r . '...' ) ; return ($ r.'...');
    5. )

    Note the "treat" for not trying to eliminate the extra space :)

  7. getAvatar 1.0
    September 22, 2008 Giovambattista Fazioli:

    @ Julius: Please dear! Although no one has noticed that for large strings to its proper function would be:

    PHP:
    1. $c , $l ) { wordCut function ($ c, $ l) (
    2. explode ( ' ' , $c ) ; $ a = explode ( '', $ c);
    3. count ( $a ) > $l ) { if (count ($ a)> $ l) (
    4. $i = 0 ; $i < $l ; $i ++ ) $r [ $i ] = $a [ $i ] ; for ($ i = 0; $ i <$ l, $ i + +) $ r [$ i] = $ a [$ i];
    5. implode ( ' ' , $r ) . '...' ; $ r = implode ( '', $ r).'...';
    6. ; return $ r;
    7. $c ; ) Else return $ c;
    8. )

    P

  8. getAvatar 1.0
  9. getAvatar 1.0
    September 13, 2009 Undolog.com "Very short snippet: PHP word cut, counting characters:

    [...] Already seen Very short snippet: PHP word cut is more pleasant to break a text to "words", because it avoids cutting a word. There [...]

Leave a comment

TAG XHTML PERMISSIONS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre>         // blocco generico [code][/code]       // blocco generico [as][/as]           // Actionscript [css][/css]         // CSS Style Sheet [html][/html]       // HTML [js][/js]           // Javascript [objc][/objc]       // Objective-C [php][/php]         // PHP [sql][/sql]         // SQL