google-code-export / rtgui

Automatically exported from code.google.com/p/rtgui
1 stars 0 forks source link

wordwrap() not multibyte friendly. #71

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
PHP's wordwrap() function splits multibyte characters in places where they
should not always be split, possibly causing some of text to be unreadable.
 There are a few comments on the manual of the function[1] with drop-in
replacements.  Here's a random picking (actually, the latest):

function mb_wordwrap($string, $width=75, $break="\n", $cut=false) {
    if (!$cut) {
        $regexp = '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){'.$width.',}\b#U';
    } else {
        $regexp = '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){'.$width.'}#';
    }
    $string_length = mb_strlen($string,'UTF-8');
    $cut_length = ceil($string_length / $width);
    $i = 1;
    $return = '';
    while ($i < $cut_length) {
        preg_match($regexp, $string,$matches);
        $new_string = $matches[0];
        $return .= $new_string.$break;
        $string = substr($string, strlen($new_string));
        $i++;
    }
    return $return.$string;
}

Not sure if mb_strlen() will cause any problems for other locales or not. 
Looks like view.php is the only file calling wordwrap(): lines 48, 93, 138,
206 (as of v0.2.6).

[1] http://www.php.net/manual/en/function.wordwrap.php

Original issue reported on code.google.com by llamaXxX@gmail.com on 12 Mar 2009 at 2:17

GoogleCodeExporter commented 9 years ago
Accepted - although I've never personally experienced the problem.

This is a bit hack-y anyway - it surely should be done via some nice CSS 
instead...

Will be included in next version.

Original comment by lemonbe...@gmail.com on 16 Mar 2009 at 9:01

GoogleCodeExporter commented 9 years ago
Fixed in version 0.2.7

Original comment by lemonbe...@gmail.com on 16 Mar 2009 at 10:19