Reputation: 20984
I am building a JS page dynamically with php using .htaccss to make .php into .js
All works well apart from the output of the JS.
IE
$data = array('one', 'two');
foreach($data as $d){
echo "document.write('This is a test for array item ".$d."'); \r\n";
}
Problem is it outputs it all on one line ie
document.write('This is a test for array one');document.write('This is a test for array two');
No matter what I have tried I cant get it over 2 lines.
Any ideas ?
Upvotes: 0
Views: 1648
Reputation: 27164
Edit: It looks like I misunderstood the question - he was having trouble with newlines in his JS, not his final HTML.
Your JavaScript is coming out on separate lines because of the "\r\n" at the end of the string, but then you're outputting plain text into your HTML document. HTML does not break lines unless you're in a pre-formatted block (like "<pre>") or you give it an explicit break (like "<br>").
You probably want your code to look like this:
foreach($data as $d){
echo "document.write('This is a test for array item ".$d."<br>'); \r\n";
}
Just be very careful of your data - inserting random strings into your HTML is a fast way to get security holes.
Upvotes: 2
Reputation: 44823
Heh, actually, I think if you view the source of the generated file you will find that the line breaks are indeed there. : )
Upvotes: 0