[PHP] preg_replace ^M
Hello i try to replace a string in a file but if i have linefeeds in the string the output file after the replacement has ^M carachters in in if the $replacements[] = test; has no \n in it all is OK $fp = fopen (draft.html, r); $incont = fread ($fp,filesize(draft.html)); fclose ($fp); $patterns[] = /\[CONTENT\]/; $replacements[] = test\nout; $content = preg_replace ($patterns,$replacements,$incont); $fp = fopen (out.html,w); fputs ($fp, $content); fclose($fp); draft.html looks like: form method=post [CONTENT] /form the out put get ugly ^M form method=post ^M SERAVS toro^M ^M /form^M BR/Torsten -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace ^M
Those are \r characters from dos newline (\r\n). Generally they are not harmful and many editors can work with them without problems (vim). You can use some utility commands to convert to or from dos or unix newlines. Torsten Rosenberger wrote: Hello i try to replace a string in a file but if i have linefeeds in the string the output file after the replacement has ^M carachters in in if the $replacements[] = test; has no \n in it all is OK $fp = fopen (draft.html, r); $incont = fread ($fp,filesize(draft.html)); fclose ($fp); $patterns[] = /\[CONTENT\]/; $replacements[] = test\nout; $content = preg_replace ($patterns,$replacements,$incont); $fp = fopen (out.html,w); fputs ($fp, $content); fclose($fp); draft.html looks like: form method=post [CONTENT] /form the out put get ugly ^M form method=post ^M SERAVS toro^M ^M /form^M BR/Torsten -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace ^M
Hello Those are \r characters from dos newline (\r\n). Generally they are not harmful and many editors can work with them without problems (vim). You can use some utility commands to convert to or from dos or unix newlines. But i'm working under Linux. I made a test with HTML Template IT and addBlockfile and thats the same. i fetched the content with tpl-get() and wrote it to a file the same form method=post action=^M ^M ^M hr Tmbp ^M ^M /form^M BR/Torsten -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace ^M
From: Torsten Rosenberger [EMAIL PROTECTED] i try to replace a string in a file but if i have linefeeds in the string the output file after the replacement has ^M carachters in in Some text editors will display \r as ^M. So, if you're file uses \r\n as the newline, you'll see these ^M at the end of each line. Using a different text editor or adjusting the properties of the one you've got should fix this. Either way, they shouldn't be visible on the actual PHP/HTML page when viewed over the web. this is an editor issue, not a PHP one, really. ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace ^M
From: Torsten Rosenberger [EMAIL PROTECTED] Those are \r characters from dos newline (\r\n). Generally they are not harmful and many editors can work with them without problems (vim). You can use some utility commands to convert to or from dos or unix newlines. But i'm working under Linux. Doesn't matter... I made a test with HTML Template IT and addBlockfile and thats the same. So that program is writing \r\n as the newline instead of just \n. It's still just your editor that's displaying the ^M. Maybe you should get a new editor. ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace ^M
So that program is writing \r\n as the newline instead of just \n. It's still just your editor that's displaying the ^M. Maybe you should get a new editor. i use vim and the input file don't have \r\n they look normal in vim after the preg_replace in php then they have the ^M BR/Torsten -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace ^M
Torsten, et al -- ...and then Torsten Rosenberger said... % % So that program is writing \r\n as the newline instead of just \n. It's % still just your editor that's displaying the ^M. Maybe you should get a new % editor. % i use vim Good :-) % % and the input file don't have \r\n they look normal in vim % after the preg_replace in php then they have the ^M I suspect that you have a DOS format file which, after replacement, has a line with only \n (no \r) which makes it a UNIX format file -- with lots of extra \r chars in there. Open your source file with vim. Type :set fileformat and hit return and see what it says (my bet is 'dos'). Type :set fileformat=unix :wq! and then run your script again and check the output (my bet is no more ^M chars). % % BR/Torsten HTH HAND :-D -- David T-G * There is too much animal courage in (play) [EMAIL PROTECTED] * society and not sufficient moral courage. (work) [EMAIL PROTECTED] -- Mary Baker Eddy, Science and Health http://justpickone.org/davidtg/ Shpx gur Pbzzhavpngvbaf Qrprapl Npg! pgp0.pgp Description: PGP signature
Re: [PHP] preg_replace ^M
Torsten Rosenberger wrote: ^M carachters in in Classical pblm of representing end of line in text files between OS: windows uses \r\n aka CRNL *nixuses \n aka NL (newline) mac uses \r aka CR (carriage return) Good text editors dont care (win: wordpad, not notepad) and can convert while reading/writing (emacs, vim, etc). --not sur for mac way. Use hex editor to know for sure what is 'the' newline char. \r is 0D in hex \n is 0A in hex $ hexdump -C file.txt | head -20 In your case, the src file contains \r\n or the file is written in text mode on a windows server, most probably. $fp = fopen (draft.html, r); $incont = fread ($fp,filesize(draft.html)); (...) $fp = fopen (out.html,w); fputs ($fp, $content); the out put get ugly ^M With files _in_text_mode_ (see flags of fopen), the \n char in PHP is virtual : following OS, PHP version, it can be written as \r, \r\n or \n. Either use non portable t flag on windows to make transparent \r\n -- \n translations, or better always use files in _binary_ mode and choose yourself your eol char (\n is simpler). The latter will improve portability. See php official doc http://www.php.net/manual/en/function.fopen.php FYI: Perl also use a 'virtual' \n char, and that can cause problems. Most of Internet protocols use \r\n as line separators, and sending only \n is asking for trouble soon or later... See perlport(1) Specific info for vim: :help dos-file-formats vim -b file.txt (read in binary mode, eol is always \n) :set ff=dos (read any, write \r\n) :set ff=unix (read dos, write \n) Not using emacs often enough to provide same info. Someone here ? It also does right things automatically, but dont know shortcuts or functions to alter that correct behavirou ;-) Hope it helps, Christophe -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php