I have files, with hundreds of lines of code written in HTML/PHP and wherever there used to be a line break, Example Line 1 Line 2 The files have turned into one line, and are set out like this Line1\nLine2\nLine3\n I need to convert them back somehow. Any help would be greatly appreciated.
foreach( scandir(".") as $file ) { $old = file_get_contents( $file ); file_put_contents( $file, str_replace( "\n", "\r\n", $old ) ); } PHP: server will need write permissions on the folder / files while you execute that code .....
<?php foreach( scandir(".") as $file ) { $old = file_get_contents( $file ); file_put_contents( $file, str_replace( "\n", "\r\n", $old ) ); } ?> I made a file called yo.php then put a few .txt documents with all the code to be fixed, set 777 permissions but get this error when executing yo.php Fatal error: Call to undefined function: scandir() in /home/server/public_html/fixsites/yo.php on line 2
missing a } ? <?php foreach( scandir(dirname(__FILE__)) as $file ) { if($file != "." && $file != ".."): $old = file_get_contents( $file ); file_put_contents( $file, str_replace( "\n", "\r\n", $old ) ); endif; } ?> PHP: Still might need different code, let me know what happens and this time if it says theres an error, post the line mentioned.
Yeah it's specifically php5, which is why I said you might need different code : if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $old = file_get_contents( $file ); file_put_contents( $file, str_replace( "\n", "\r\n", $old ) ); } } closedir($handle); } PHP:
Fatal error: Call to undefined function: file_put_contents() in /home/server/public_html/fixsites/yo.php on line 6
<?php if(!function_exists("file_put_contents") ) { function file_put_contents( $file, $data ) { $handle = fopen($file, "w+"); if(!$handle) : return false; endif; if(!fwrite( $handle, $data, strlen($data))): return false; endif; fclose($handle); return true; } } if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $old = file_get_contents( $file ); file_put_contents( $file, str_replace( "\n", "\r\n", $old ) ); } } closedir($handle); } ?> PHP: ???
I installed PHP on my computer, so I could run these snippets, and all of them dump an empty file. I have it set up like this fix.php fix\ inside fix\ is a file called test.txt and in text.txt is jumbled one line code, with \n where there used to be line drops. When I run fix.php, it dumps a file like this fix.php fix\ test.txt and inside test.txt theres NOTHING. Almost there! Thanks heaps for your help so far too!
code needs to be in the same folder as the files being modified. Also, wouldn't bother writing \n in any files to test, download a select few from your site to see what happens. Failing that, send me a zip with a few of these files in so I can have a looksee for myself .....
ok, i've tried that aswell, having all in the same folder. The code looks like this: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" ><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" >\n<head>\n<title><? echo $title ?></title>\n<?php \nif ( $top == ""){\n $top = " but it just keeps on going and going in one line. See all the \n 's ? They should be line drops
ok it NEEDS to be in the same folder as the files, and do not chmod this file to 777 only the html files. <?php if(!function_exists("file_put_contents") ) { function file_put_contents( $file, $data ) { $handle = fopen($file, "w+"); if(!$handle) : return false; endif; if(!fwrite( $handle, $data, strlen($data))): return false; endif; fclose($handle); return true; } } if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $old = file_get_contents( $file ); file_put_contents( $file, str_replace( '\n', "\n", $old ) ); } } closedir($handle); } ?> PHP: It works, I tested it .....
JosS: you can't just type '\n' into a text editor and expect it to work... '\n' is a special character that you can't just type. It's a control character: if you can see the characters '\' and 'n', then that is not the same as a '\n' character.
Yeah, I ended up figuring out that, I had to do a normal search and replace in notepad to replace the \n's to something like poop, then use the PHP script to replace poop to \r\n. If that made sense.