Bug #55842 [Opn]: Explode function sensitive to data!

From: Date: Tue, 04 Oct 2011 15:29:43 +0000
Subject: Bug #55842 [Opn]: Explode function sensitive to data!
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-164087@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=55842&edit=1 ID: 55842 User updated by: lennart dot ohrstedt at diadoker dot se Reported by: lennart dot ohrstedt at diadoker dot se Summary: Explode function sensitive to data! Status: Open Type: Bug Package: Strings related Operating System: Mac OS x 10.7.1,MAMP 1.9.6 PHP Version: 5.3SVN-2011-10-04 (SVN) Block user comment: N Private report: N New Comment: The test_new is originally a html file with php.code inside, and have been trimmed by the trim function to get rid of the blanks. The outcome is that test2.php should contain ALL the information in the test_new.php changed by the explode to have one html kommand per line. Previous Comments: ------------------------------------------------------------------------ [2011-10-04 15:21:03] lennart dot ohrstedt at diadoker dot se Test_new.php contains this: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd."><!-- created by Lennart Öhrstedt, Diadoker--><html xmlns="http://www.w3.org/1999/xhtml" lang="sv" xml:lang="sv"><head><title>Min mall</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859- 1" /><meta name="description" content="" /><meta name="keywords" content="" /> <meta name="author" content="Lennart Öhrstedt" /><meta name="Copyright" content="Diadoker" /><meta name="Company" content="Diadoker" /><meta http- equiv="Language" content="Sv" /><meta name="Robots" content="index,follow" /> <link rel="shortcut icon" href="images/alexander4.jpg" type="image/x-icon" /> <link rel="stylesheet" href="css/std.css" type="text/css" /><style type="text/css"></style><script type="text/javascript" src="js/mall.js"></script> <script type="text/javascript" src="js/jquery1.6.2.min.js"></script><script type="text/javascript" language="JavaScript"></script></head><body onload=""> <script>$(document).ready(function(){resizeWindow();});</script><?php function customError($errno, $errstr){echo "<b>Error:</b> [$errno] $errstr";}set_error_handler("customError");if ( file_exists("index.html") ) {$file_in = fopen("index.html","r+");$file_out = fopen("index_new.html","w+");}$keyframes=array();$typ=array("-moz-","-o-","-ms- ","");while(!feof($file_in)){$line_in=fgets($file_in); /* $line_in=ltrim(rtrim(fgets($file_in))); */fwrite($file_out,$line_in);if (! strstr($line_in,"@-webkit-")&& strstr($line_in,"-webkit-")) {$line_out=str_replace("-webkit-","-moz- ",$line_in);fwrite($file_out,$line_out);$line_out=str_replace("-webkit-","-o- ",$line_in);fwrite($file_out,$line_out);$line_out=str_replace("-webkit-","-ms- ",$line_in);fwrite($file_out,$line_out);$line_out=str_replace("-webkit- ","",$line_in);fwrite($file_out,$line_out);}if (strstr($line_in,"@-webkit-")) {$line_count=1;$i=0;$keyframes[$i]=$line_in; //spara rad nwhile ($line_count >0) {$i++;$line_in=fgets($file_in); // läs rad n+1fwrite($file_out,$line_in); // skriv rad n+1$keyframes[$i]=$line_in; //spara rad n+1if (strstr($line_in," {"))$line_count++;if (strstr($line_in,"}"))$line_count--;}for ($j=0; $j<=3; $j++) // 4 rundor för -moz-, -o., -ms- och ""{for ($k=0; $k<=$i; $k++) //för att skriva det sparade för respektive webbläsare{$line_out=$keyframes[$k];if (strstr($keyframes[$k],"-webkit-"))$line_out=str_replace("-webkit- ",$typ[$j],$keyframes[$k]);fwrite($file_out,$line_out);}}}}fclose($file_in);fclos e($file_out);/* rename("index.html","index_old.html");rename("index_new.html","index.html"); */?><div align="center"><hr width="90%" /><p>Nu skall det vara gjort</p><hr width="90%" /><table width="100%" border="0" cellpadding="0" cellspacing="0" summary="Copyright"><tr><td align="center"><span style='font-size:10.0pt;font- family:Arial;color:#000000;'>Copyright &copy; 2011 Diadoker. All rights reserved. </span><br /><a href="mailto:lennart.ohrstedt@diadoker.se" style="color: black">lennart.ohrstedt@diadoker.se</a><br /><a href="http://www.diadoker.se/" style="color: black" target="_new">www.diadoker.se</a></td></tr></table></div> </body></html> The file test2.php contain this :function customError($errno, $errstr){echo "<b> Error:</b> [$errno] $errstr";}set_error_handler("customError");if ( file_exists("index.html") ){$file_in = fopen("index.html","r+");$file_out = fopen("index_new.html","w+");}$keyframes=array();$typ=array("-moz-","-o-","-ms- ","");while(!feof($file_in)){$line_in=fgets($file_in); /* $line_in=ltrim(rtrim(fgets($file_in))); */fwrite($file_out,$line_in);if (! strstr($line_in,"@-webkit-")&& strstr($line_in,"-webkit-")) {$line_out=str_replace("-webkit-","-moz- ",$line_in);fwrite($file_out,$line_out);$line_out=str_replace("-webkit-","-o- ",$line_in);fwrite($file_out,$line_out);$line_out=str_replace("-webkit-","-ms- ",$line_in);fwrite($file_out,$line_out);$line_out=str_replace("-webkit- ","",$line_in);fwrite($file_out,$line_out);}if (strstr($line_in,"@-webkit-")) {$line_count=1;$i=0;$keyframes[$i]=$line_in; //spara rad nwhile ($line_count > 0){$i++;$line_in=fgets($file_in); // läs rad n+1fwrite($file_out,$line_in); // skriv rad n+1$keyframes[$i]=$line_in; //spara rad n+1if (strstr($line_in," {"))$line_count++;if (strstr($line_in,"}"))$line_count--;}for ($j=0; $j<=3; $j++) // 4 rundor för -moz-, -o., -ms- och ""{for ($k=0; $k<=$i; $k++) //för att skriva det sparade för respektive webbläsare{$line_out=$keyframes[$k];if (strstr($keyframes[$k],"-webkit-"))$line_out=str_replace("-webkit- ",$typ[$j],$keyframes[$k]);fwrite($file_out,$line_out);}}}}fclose($file_in);fclos e($file_out);/* rename("index.html","index_old.html");rename("index_new.html","index.html"); */?> <div align="center"> <hr width="90%" /> <p> Nu skall det vara gjort</p> <hr width="90%" /> <table width="100%" border="0" cellpadding="0" cellspacing="0" summary="Copyright"> <tr> <td align="center"> <span style='font-size:10.0pt;font-family:Arial;color:#000000;'> Copyright &copy; 2011 Diadoker. All rights reserved.</span> <br /> <a href="mailto:lennart.ohrstedt@diadoker.se" style="color: black"> lennart.ohrstedt@diadoker.se</a> <br /> <a href="http://www.diadoker.se/" style="color: black" target="_new"> www.diadoker.se</a> </td> </tr> </table> </div> </body> </html> The test2.php is the result of explode test_new.php. ------------------------------------------------------------------------ [2011-10-04 15:12:28] pajoye@php.net What contain "test_new.php" and "test2.php"? And what are the expected and actual results? ------------------------------------------------------------------------ [2011-10-04 13:49:21] lennart dot ohrstedt at diadoker dot se <?php $fil_in = "test_new.php"; $fil_out = "test2.php"; if ( file_exists($fil_in) ) { $file_in = fopen($fil_in,"r+"); $file_out = fopen($fil_out,"w+"); } while(!feof($file_in)) { $line_in=fgets($file_in); $line_array=explode(">",$line_in); } $j=count($line_array); for ($i=0;$i<($j-1);$i++) { $line_out=$line_array[$i]; fwrite($file_out,$line_out.">"."\n"); } fclose($file_in); fclose($file_out); ?> ------------------------------------------------------------------------ [2011-10-04 12:58:19] pajoye@php.net Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. A proper reproducing script starts with <?php and ends with ?>, is max. 10-20 lines long and does not require any external resources such as databases, etc. If the script requires a database to demonstrate the issue, please make sure it creates all necessary tables, stored procedures etc. Please avoid embedding huge scripts into the report. ------------------------------------------------------------------------ [2011-10-04 11:38:23] lennart dot ohrstedt at diadoker dot se Description: ------------ If I have a string containing "<?php" somewhere in the middle and I use explode to split the string. Then the output array will not have the part of the string prior to the "<?php", the output array will start at the character following the "<?php". Is this a bug or this is the way explode works? Test script: --------------- use URL www.diadoker.se/test/packa_upp.php. The input file, "test_new.php", to this is a previus "packed" php-file using "trim", and the output is to file "test2.php". Expected result: ---------------- I expected the result to be equal to the input but split according to the parameters in the explode function. Actual result: -------------- When there is the characters "<?php" somewhere in the string, everything before these characters are lostand the first character in the output array is the character following the "<?php". ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=55842&edit=1

« previous php.bugs (#164087) next »