Make WordPress Core

Ticket #5320: strlen.diff

File strlen.diff, 2.0 KB (added by ryan, 18 years ago)
  • wp-includes/formatting.php

     
    9494
    9595
    9696function seems_utf8($Str) { # by bmorel at ssi dot fr
    97         for ($i=0; $i<strlen($Str); $i++) {
     97        $length = strlen($Str);
     98        for ($i=0; $i < $length; $i++) {
    9899                if (ord($Str[$i]) < 0x80) continue; # 0bbbbbbb
    99100                elseif ((ord($Str[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb
    100101                elseif ((ord($Str[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb
     
    103104                elseif ((ord($Str[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b
    104105                else return false; # Does not match any model
    105106                for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
    106                         if ((++$i == strlen($Str)) || ((ord($Str[$i]) & 0xC0) != 0x80))
     107                        if ((++$i == $length) || ((ord($Str[$i]) & 0xC0) != 0x80))
    107108                        return false;
    108109                }
    109110        }
     
    132133        $unicode = '';
    133134        $values = array();
    134135        $num_octets = 1;
     136        $unicode_length = 0;
    135137
    136         for ($i = 0; $i < strlen( $utf8_string ); $i++ ) {
     138        $string_length = strlen( $utf8_string );
     139        for ($i = 0; $i < $string_length; $i++ ) {
    137140
    138141                $value = ord( $utf8_string[ $i ] );
    139142
    140143                if ( $value < 128 ) {
    141                         if ( $length && ( strlen($unicode) + 1 > $length ) )
     144                        if ( $length && ( $unicode_length >= $length ) )
    142145                                break;
    143146                        $unicode .= chr($value);
     147                        $unicode_length++;
    144148                } else {
    145149                        if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3;
    146150
    147151                        $values[] = $value;
    148152
    149                         if ( $length && ( (strlen($unicode) + ($num_octets * 3)) > $length ) )
     153                        if ( $length && ( $unicode_length + ($num_octets * 3) ) > $length )
    150154                                break;
    151155                        if ( count( $values ) == $num_octets ) {
    152156                                if ($num_octets == 3) {
    153157                                        $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]);
     158                                        $unicode_length += 9;
    154159                                } else {
    155160                                        $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]);
     161                                        $unicode_length += 6;
    156162                                }
    157163
    158164                                $values = array();