1

I'm writing a script in PHP to change some codes in HTML files. Every file will be read in line per line and thus every operation has to be per line. My problem is that some operations lose the newline character if it is the end of the file.

Just to be clear, I don't want to have HTML newline characters (<br />) but I want normal newline characters (\n) to get a better formatted code inside the code editor.

HTML example before:

<body>
<div id="wrapper">

HTML example after:

<body>
<div id="jobtempl"> <!-- jobtempl --><div id="wrapper">

How it should be:

<body>
<div id="jobtempl"> <!-- jobtempl -->
<div id="wrapper">

PHP Code:

private function replaceContent($file) {
    $this->file = $file;
    $lines = file($this->file);

    // some stuff to get $bodyStartLine as line number where to add the code

    $handle = fopen($this->file, 'w');

    foreach ($lines as $line) {
        if ($i == $bodyStartLine) {
            $line = $line . '<div id="jobtempl"> <!-- jobtempl -->';
        }

        if ($i == $bodyEndLine) {
            $line = '</div> <!-- /jobtempl -->' . PHP_EOL . $line; // works
        }

        fwrite($handle, $line);
    }

    fclose($handle);
}

Even if I use $line = $line . '<div id="jobtempl"> <!-- jobtempl -->' . "\n"; to force a newline character it doesn't work. The same with \r\n\ and \r.

What works is when I later close this new div:

$line = '</div> <!-- /jobtempl -->' . "\n" . $line;

So whenever there is something after the newline character to add, it will work. Is it possible to get it working without adding new content after the newline character?

KittMedia
  • 7,368
  • 13
  • 34
  • 38
  • 3
    I normally would try using PHP_EOL here us a good link to look at http://stackoverflow.com/questions/128560/when-do-i-use-the-php-constant-php-eol – Programmer man Oct 08 '14 at 14:04
  • PHP_EOL just accounts for the OS php is running on, ie it will use \n for unix or \r\n for windows, \r for mac I think we need the full excerpt of your code here to help. – shazbot Oct 08 '14 at 14:09
  • Doesn't even work by using it this way: `$line = $line . '
    ' . PHP_EOL;`
    – KittMedia Oct 08 '14 at 14:10
  • What transformations are going on in the rest of your code? It sounds like there's something else affecting the new lines. Have you checked to see when the new lines are disappearing from the `$line` string--e.g. by echoing it at the end of the `foreach` loop? – i alarmed alien Oct 08 '14 at 14:24
  • I added more code. There are no other transformations except of a line counter to get the correct position where to add the div. – KittMedia Oct 08 '14 at 14:33

1 Answers1

1

Take a look to this answer on SO

echo 'Hello, world!' . "\xA";

UPDATE

I tried your code and it needs some extra new line characters. With PHP_EOL and \xA works fine.

See this code: http://ideone.com/ZQHTqj

Community
  • 1
  • 1
rogelio
  • 1,541
  • 15
  • 19
  • Unfortunately, it doesn't even work: `$line = $line . '
    ' . "\xA";`
    – KittMedia Oct 08 '14 at 14:25
  • I think your problem is not with the new line character. See my update, maybe you need to add some extra characters at the start or end when appropriate. – rogelio Oct 08 '14 at 18:12
  • The problem is that I don't want to add an extra character because then the formatting is wrong. – KittMedia Oct 09 '14 at 06:48