Vectorized over str
.Any of the strings must not contain \r
, \n
,
or other new line characters, otherwise you will get at error.
You should split the input text into lines
or e.g. substitute line breaks with spaces
before applying this function.
Multiple white spaces between the word boundaries are
preserved withing each wrapped line. If you wish to substitute
all such sequences with single spaces, call e.g.
stri_trim(stri_replace_all_charclass(str, "\p{WHITE_SPACE}", " ", TRUE))
prior to string wrapping.
The greedy algorithm (for cost_exponent
being non-positive)
provides a very simple way for word wrapping.
It always puts as many words in each line as possible.
This method -- contrary to the dynamic algorithm -- does not minimize
the number of space left at the end of every line.
The dynamic algorithm (a.k.a. Knuth's word wrapping algorithm)
is more complex, but it returns text wrapped
in a more aesthetic way. This method minimizes the squared
(by default, see cost_exponent
) number of spaces (raggedness)
at the end of each line, so the text is mode arranged evenly.
Note that Unicode code points may have various widths when
printed on screen. This function acts like each code point
is of width 1. This function should rather be used with
text in Latin script.
ICU's line-BreakIterator
is used to determine
text boundaries at which a line break is possible.