As seen in the Very short snippet: PHP word cut is more pleasant to break a text to "words", as it avoids cutting a word. There is still counting those who prefer to cut the characters, as it makes the "cuts" more homogeneous and similar to each other, despite the significant risk of cutting a word, severed the net! In addition you should also consider that, with the exception for special cases, the fonts are not proportional and therefore the word "www" can occupy a horizontal space in pixels than "Wii".
Similarly, when there are words you may run into situations paraddosali, generating apparent differences between the texts that sìvoglione break. For example, the phrase "face to face" contains four words, just as "Washington, the White House." However, it is clear that the last sentence is visibly more "long", in pixels.
A possible solution to the problem is a hybrid function, then taking into account both the length of characters, both the "cut" at the end of the word:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | / ** * Cuts a string to the characters words cash * In this version are not "counted" spaces * Uncomment the line if you want a cut * Exact character * * @ Author = undo = * @ Date 11/09/2009 * * @ Param (string) $ string to c cutting * @ Param (int) $ the maximum number of characters * @ Param (string) $ and string to hang, default "[...]" * @ Return * / $c , $l , $e = "[...]" ) { word_cut_for_chars function ($ c, $ s, $ e = "[...]") { strlen ( $c ) > $l ) { if ( strlen ($ c)> $ l) { explode ( ' ' , $c ) ; $ A = explode ('', $ c); 0 ; $ S = 0; "" ; $ R = ""; $i = 0 ; $i < count ( $a ) ; $i ++ ) { for ($ i = 0; $ i < count ($ a) $ i + +) { strlen ( $a [ $i ] ) ; $ S + = strlen ($ a [$ i]); / / $ S + = (strlen ($ a [$ i]) + 1) / / also has "space" $s > $l ) return ( $r . $e ) ; if ($ s> $ l) return ($ r. $ s); $a [ $i ] . " " ; $ R .= $ a [$ i]. '" } } ; return $ c; } |










Small error in the title "Cash"

Semra useful your posts!
@ Raben: That's what happens in the small hours
Thanks
Hello I will use this solution for some time:
2
3
4
5
6
7
8
9
strip_tags ( $text ) ; //Elimino eventuali tag HTML $ Text = strip_tags ($ text); / / Delete any HTML tags
strlen ( $text ) <= ( int ) $len ) { if ( strlen ($ text) <= (int) $ len) {
; return $ text;
{ Else {}
wordwrap ( $text , ( int ) $len , "___end___" ) ; $ Text = wordwrap ($ text, (int) $ len, "___end___");
( $text , 0 , - strlen ( stristr ( $text , "___end___" ) ) ) . $textContinue ; return substr ($ text, 0, - strlen ( stristr ($ text, "___end___"))). textContinue $;
}
}
You should measure the time taken by both (perhaps with long strings) and understand which solution is best.
: @ Santino Bivacqua: interesting ... around I found a variant that uses
explode(), in practice, instead of yourstristr():2
3
4
5
6
7
8
9
10
11
strlen ( $text ) > $limit ) { if ( strlen ($ text)> $ limit) {
wordwrap ( $text , $limit , '[cut]' ) ; $ Txt1 = wordwrap ($ text, $ limit, '[cut]');
explode ( '[cut]' , $txt1 ) ; $ Txt2 = explode ('[cut]', $ txt1);
$txt2 [ 0 ] ; $ $ Txt2 ourTxt = [0];
$ourTxt . $msg ; $ = $ FinalTxt ourTxt. $ Msg;
{ Else {}
$text ; FinalTxt $ = $ text;
}
; return $ finalTxt;
}
@ Giovambattista Fazioli:

In fact, the principle of operation is very similar
Now there are three solutions to try to verify (in long texts) as the fastest