|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-10-04 11:38 UTC] 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". PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 08 01:00:01 2025 UTC |
<?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); ?>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'));