Edit report at https://bugs.php.net/bug.php?id=55842&edit=1
ID: 55842
Comment by: php dot net at doppy dot nl
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:
When one aligns the code posted on 2011-10-04 13:49 UTC properly, we get the
following code:
----------------------------------------------
<?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);
----------------------------------------------
This shows clearly that only the last line of the source file will be written
to
the destination file.
I think this just happens to be near a "<?php" in the file.
Thus not a bug in PHP, but in the code supplied.
Recommendation:
Closing as "not a bug".
Sidenote:
Intended result can be achieved with:
file_put_contents('test2.php', str_replace(">", ">\n",
file_get_contents('test_new.php'));
Previous Comments:
------------------------------------------------------------------------
[2011-10-04 15:29:43] lennart dot ohrstedt at diadoker dot se
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.
------------------------------------------------------------------------
[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 © 2011 Diadoker. All rights
reserved.
</span><br /><a href="mailto:[email protected]" style="color:
black">[email protected]</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 © 2011 Diadoker. All rights reserved.</span>
<br />
<a href="mailto:[email protected]" style="color: black">
[email protected]</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] [email protected]
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] [email protected]
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.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=55842
--
Edit this bug report at https://bugs.php.net/bug.php?id=55842&edit=1