html - PHP string operations lose newline characters -


I am writing a script in PHP to change some code in HTML files. Every file will be read in line per line And thus every operation should be per line. My problem is that some operations lose the new line if it is the end of the file.

Just to be clear, I want to get new characters in the HTML ( & lt; br / & gt; ) but to get a better format code inside the code editor, Newline characters ( \ n ) are required.

Example of the first HTML: & lt; body & gt; & Lt; Div id = "wrapper" & gt;

Example of HTML after this:

  & lt; body & gt; & Lt; Div id = "jobtempl" & gt; & Lt ;! - JobTemple - & gt; & Lt; Div id = "wrapper" & gt;   

How should this happen:

  & lt; body & gt; & Lt; Div id = "jobtempl" & gt; & Lt ;! - jobtempl - & gt; & Lt; Div id = "wrapper" & gt;   

PHP code:

  Change the personal function resource ($ file) {$ this- & gt; File = $ file; $ Lines = file ($ -> -; // to get some $ $ bodyStartLine as line number, where to add $ handle = fopen ($ this- & gt; file, 'w'); Currency ($ line as line $) {if ($ i == $ bodyStartLine) {$ line = $ line '& lt; div id = "jobtapple"> gt; & lt;! - jobtempl - & gt; ;} If ($ i == $ bodyEndLine) {$ line = '& lt; / div & gt; & lt ;! - / jobtempl - & gt; PHP_EOL $ line; // works} Fwrite ($ Handle, $ line);} fclose ($ handle);}   

Even if I use the $ line = $ line '& lt; div id = "JobTemple" & gt; & lt ;! - Jobtempl ->. \ "\ N"; This does not work for implementing a new line \ r \ n \ and \ r \ / code>

What works when I close this new device later:

  $ line = '& lt; / div & gt;     

So whenever there is something after adding a new line, it will work whether the new line Is it possible to work without adding new material?

Take a look at this

  echo 'hello, world!' . "\ XA";   

UPDATE

I tried your code and it requires some additional new line characters. Works fine with PHP_EOL and \ xA .

See this code:

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -