Twig, the flexible, fast, and secure template language for PHP
PHP HTML
Latest commit dbbb740 Jan 17, 2017 @fabpot fabpot bug #2366 Use null rather than specific length for 'to the end' in mb…
…_substr (arjenm)

This PR was merged into the 2.x branch.

Discussion
----------

Use null rather than specific length for 'to the end' in mb_substr

We use Twig with a ISO-8859-15 charset and noticed the 'capitalize'-method only yielded the first character of a string. I.e. "abcd" became "A" rather than "Abcd".

This was due to a change in the way mb_substr is used. Appearantly, using 2147483647 as a length to indicate 'everything to the end' only works with UTF-8 (and possibly other multibyte charsets). It probably triggers some internal overflow in php for at least ASCII and ISO-8859-15.

Luckily, an easy fix is available since PHP 5.4.8: just use 'null'

See these two tests for which version does what:
(old) https://3v4l.org/qdpIK
(new) https://3v4l.org/kivqK

I also used null in the slice-method. While it did not suffer from this same issue, it did an unnecessary mb_strlen. And there might be similar overflow issues with negative values of $start, but I didn't actually test that.

Commits
-------

3bea610 Use null rather than specific length for 'all characters to the end' with mb_substr

README.rst

Twig, the flexible, fast, and secure template language for PHP

Twig is a template language for PHP, released under the new BSD license (code and documentation).

Twig uses a syntax similar to the Django and Jinja template languages which inspired the Twig runtime environment.

More Information

Read the documentation for more information.