Wordwrap : wrong behavior when preserving #49

Open
wants to merge 1 commit into
from

Conversation

Projects
None yet
2 participants

If "mb_get_info" exist, there is a special "twig_wordwrap_filter" function not using the php default php wordwrap function. I don't know why it has been done this way, i guess using multibyte string is faster or better regarding encoding support than with the default php wordwrap function.

Anyway, the behavior of this custom function is wrong when $preserve = true, the string is simply the same before and after the filter is applied to it. Also, correct me if i'm wrong but, mb_split use $separator as its separator, the separator used in wordwrap is only supposed to be what is inserted at the end of each new string segments, not what is used to split.

My pull request is trivial and i imagine it won't be the kind of fix you want for this, but i at least wanted to point out this problem.

It's been done this way because the native PHP function wordwrap is not multibyte safe. And yes, you're right that it does not return the same result as wordwrap, but neither does your function. In the case you set preserve to true you get:

echo wordwrap('A very long woooooooooooord.', 8, "\n", true); 

A very
long
wooooooo
ooooord.

{{ 'A very long woooooooooooord.'|wordwrap(8, "\n", true) }}

A very
long
wooooooo
ooooord.

that is correct (though you should call wordwrap without negating preserve), but on the case you set it to false look what it happens:

echo wordwrap('A very long woooooooooooord.', 8, "\n", false); 

A very
long
woooooooooooord.

{{ 'A very long woooooooooooord.'|wordwrap(8, "\n", false) }}

A very l
ong wooo
oooooooo
ord.

@gjuric gjuric referenced this pull request Nov 15, 2016

Open

Make wordwrap consistent #199

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment