From:             code-it at mail dot ru
Operating system: all
PHP version:      5.3.0
PHP Bug Type:     Unknown/Other Function
Bug description:  wordwrap works incorrectly

Description:
------------
wordwrapping suppose that

wordwrap(wordwrap($s,$n,$d),$n,$d) is equal to wordwrap($s,$n,$d) and yes,
sometimes php works that way:

$s0 = 'a a a a a a a a';
$s1 = wordwrap($s0,7,'XX');
$s2 = wordwrap($s1,7,'XX');

gives $s1==$s2=='a a a aXXa a a a'


but if we add an extra space to the end of $s0, the result will be
'a a a aXXa a a aXX' != 'a a a aXXa a aXXaXX'


if we construct $s0 as (a )+a? we get $s1!=$s2 if and only if
strlen($s0)%8==0. more, the diff between $s1 and $s2 every time is the
same: trailing 'XXaXX' in $s2 instead of ' aXX' in $s1.


----------------------------

now lets take a look at the case of 1-char-length thing as the 3rd
argument to wordwrap


$s0 = 'a a a a a a a a a a a'; // as above, $s0 is constructed as (a )+a?
$s1 = wordwrap($s0,5,'X');
$s2 = wordwrap($s1,5,'X');

here we only get $s1==$s2 having strlen($s0)<11!


this bug is related to line 828 in
/php-src/branches/PHP_5_3/ext/standard/string.c?revision=287264:

current line:   laststart = lastspace = current;
should be:              laststart = lastspace = current+1;

+1 is obviuos: if we found a breakchar in the string, we can forget about
everything to the left of it INCLUDING itself and restart the whole
algorythm at the NEXT character as the first character in our new string.
so the very first `laststart = lastspace = 0;` to start becomes `laststart
= lastspace = current+1;` to restart. this patch completely removes the
strange behaviour.



when strlen(3rd argument to wordwrap)>1 another section of code works and
the above patch doesnt affect it

Reproduce code:
---------------
<?
function f( $len, $breakstr, $n) {
        echo "\n-------- \$len=$len, \$breakstr='$breakstr', \$n=$n -------\n";
        for ( $i=1, $c = 'a ', $s0='', $x=1, $k=0; $i<=$n; $i++) {
                $s0 .= $c[$x^=1];
                $s1 = wordwrap($s0,$len,$breakstr);
                $s2 = wordwrap($s1,$len,$breakstr);
                if ($s1==$s2) {
                        $k++;
                        continue;
                }
                echo "strlen(\$s0)=$i -> FAIL\n";
        }
        if ($k==$n) {
                echo "passed all, no fails\n";
        }
}
f(5,'X',20);
f(7,'XX',50);
?>

Expected result:
----------------
-------- $len=5, $breakstr='X', $n=20 -------
passed all, no fails

-------- $len=7, $breakstr='XX', $n=50 -------
passed all, no fails


Actual result:
--------------
-------- $len=5, $breakstr='X', $n=20 -------
strlen($s0)=11 -> FAIL
strlen($s0)=12 -> FAIL
strlen($s0)=13 -> FAIL
strlen($s0)=14 -> FAIL
strlen($s0)=15 -> FAIL
strlen($s0)=16 -> FAIL
strlen($s0)=17 -> FAIL
strlen($s0)=18 -> FAIL
strlen($s0)=19 -> FAIL
strlen($s0)=20 -> FAIL

-------- $len=7, $breakstr='XX', $n=50 -------
strlen($s0)=8 -> FAIL
strlen($s0)=16 -> FAIL
strlen($s0)=24 -> FAIL
strlen($s0)=32 -> FAIL
strlen($s0)=40 -> FAIL
strlen($s0)=48 -> FAIL


-- 
Edit bug report at http://bugs.php.net/?id=49361&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=49361&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=49361&r=trysnapshot53
Try a snapshot (PHP 6.0):            
http://bugs.php.net/fix.php?id=49361&r=trysnapshot60
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=49361&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=49361&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=49361&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=49361&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=49361&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=49361&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=49361&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=49361&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=49361&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=49361&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=49361&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=49361&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=49361&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=49361&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=49361&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=49361&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=49361&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=49361&r=mysqlcfg

Reply via email to