Very short snippet: PHP word cut

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

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

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

  1. September 21, 2008 Tom :

    From 2 lines for a esempiuccio you could do it. :)

  2. September 21, 2008 Napolux :

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

    What do you say?

  3. 22 September 2008 Julius:

    Thank you! Carpita and used ...

  4. September 22, 2008 Giovambattista Fazioli :

    @ Tom: oh my God, an example for a function "string size" is a bit 'too ... :) However, for example, you can use to:

    1
    wordCut ( 'Stringa di esempio abbastanza lunga per essere tagliata' , 5 ) ; $ Res = wordCut ('string as long enough to be cut', 5);

    You will get in the $res :

      Sample string long enough ... 
  5. September 22, 2008 Giovambattista Fazioli :

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

    1
    2
    3
    4
    5
    $c , $l ) { wordCut function ($ c, $ s) {
    explode ( ' ' , $c ) ; $ C = explode ('', $ c);
    $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] );
    $r . '...' ) ; return ($ r.'...');
    }

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

  6. September 22, 2008 Giovambattista Fazioli :

    @ Julius: I pray dear! Although no one realized that the proper function for large string would be:

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

    : P

  7. September 13, 2009 Undolog.com "Very short snippet: PHP, cut into words, counting the characters :

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

Leave a comment

XHTML TAG PERMIT: <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 [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL