Using substr() on a UTF8 string that contains multibyte characters may produce an invalid UTF8 string.
For example, this code may produce an invalid string (will result in "warning: htmlspecialchars(): Invalid multibyte sequence in argument in /.../bootstrap.inc on line 857."):
if (strlen($block_description) > 16) {
$block_description = substr($block_description, 0, 16) . '...';
}
and can be changed to this to solve the problem:
if (drupal_strlen($block_description) > 16) {
$block_description = truncate_utf8($block_description, 16, FALSE, TRUE);
}
The bug can be reproduced with the following translation:
msgid "None"
msgstr "טרם ניצפה"
The attached patch makes this change for the three instances I could find where this potential problem is apparent.
There are other uses of substr() which are either not operating on a translated strings or which I was not sure about.
Comments
Comment #1
geneticdrift commentedComment #2
dawehnerSadly this patch doesn't apply anymore to 6.x-3.x, but it definitive seems to make sense.
In general patches like that aren't committed to 6.x-2.x anymore.
Comment #3
damien tournoud commentedYou want
drupal_strlen()anddrupal_substr(), orstrlen()andtruncate_utf8()but not another mix of them.truncate_utf8()is designed to truncate a UTF8 string on a specified number of *bytes*. If you want to truncate on a specified number of *characters*, usedrupal_substr().Comment #4
damien tournoud commentedFact-checking #3, this is not true anymore since Drupal 6. Sorry about the noise.
Comment #5
dawehnerBased on http://drupal.org/node/1093636#comment-4235430
Comment #6
mustanggb commented