Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Wordwrap : wrong behavior when preserving #49

Open
wants to merge 1 commit into from

2 participants

@jcrombez

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.

@sdepablos

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 16, 2012
  1. @jcrombez
This page is out of date. Refresh to see the latest.
Showing with 19 additions and 15 deletions.
  1. +19 −15 lib/Twig/Extensions/Extension/Text.php
View
34 lib/Twig/Extensions/Extension/Text.php
@@ -69,22 +69,26 @@ function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $sepa
{
$sentences = array();
- $previous = mb_regex_encoding();
- mb_regex_encoding($env->getCharset());
-
- $pieces = mb_split($separator, $value);
- mb_regex_encoding($previous);
-
- foreach ($pieces as $piece) {
- while(!$preserve && mb_strlen($piece, $env->getCharset()) > $length) {
- $sentences[] = mb_substr($piece, 0, $length, $env->getCharset());
- $piece = mb_substr($piece, $length, 2048, $env->getCharset());
- }
-
- $sentences[] = $piece;
+ if(!$preserve) {
+ $previous = mb_regex_encoding();
+ mb_regex_encoding($env->getCharset());
+
+ $pieces = mb_split($separator, $value);
+ mb_regex_encoding($previous);
+
+ foreach ($pieces as $piece) {
+ while(mb_strlen($piece, $env->getCharset()) > $length) {
+ $sentences[] = mb_substr($piece, 0, $length, $env->getCharset());
+ $piece = mb_substr($piece, $length, 2048, $env->getCharset());
+ }
+
+ $sentences[] = $piece;
+ }
+
+ return implode($separator, $sentences);
+ } else {
+ return wordwrap($value, $length, $separator, true);
}
-
- return implode($separator, $sentences);
}
} else {
function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...')
Something went wrong with that request. Please try again.