...

View Full Version : echo raw html with php



shadkeene
03-10-2010, 06:32 PM
Hi,

I'm trying to echo a string that utilizes html to present newlines, and also retains some actual <br> for html use later.

Here's the code:

echo "var point = new GLatLng($latlon);<br>
var marker = createMarker(point,\"$number<br>$address\")<br>
mmap.addOverlay(marker);<br>";

And I'd like the output to be:

var point = new GLatLng(43.4026,-124.262 );
var marker = createMarker(point,"CO29<br>STREET,COOS BAY ")
mmap.addOverlay(marker);

Right now, the output is the following because its putting a newline where the <br> is:
var point = new GLatLng(43.4026,-124.262 );
var marker = createMarker(point,"CO29
STREET,COOS BAY ")
mmap.addOverlay(marker);

I'd like to retain the <br> the way it is, for future use by HTML. I've tried all kinds of methods: escaping, single quotes versus double, concatenation, but can't get the right way to do it.

Thanks very much for your time!

Dormilich
03-10-2010, 06:51 PM
it looks like you want to display that JS code … then you have to escape all tags (&lt;br&gt; ). on the other hand, if you serve the file as plain text, there will be no parsing of tags.

Fou-Lu
03-10-2010, 06:56 PM
<br> will only be interpreted as a break in an html output. It will appear as the actual string <br> when viewed as source.
For newline source to work in you're javascript, you need to use either PHP_EOL or just a \n. If this is to be text to be displayed within html, you can escape it with htmlspecialchars to convert > to &gt; and < to &lt;. You would do this either with printing it as text or into a <textarea> for example. htmlspecialchars will also convert the double quotations to &quot;, but you still need the escape character for the php. The entire string must be in double quotes if you want to parse the \n as a newline.



echo htmlspecialchars("var point = new GLatLng($latlon);\n
var marker = createMarker(point,\"$number<br>$address\")\n
mmap.addOverlay(marker);\n");

shadkeene
03-10-2010, 09:59 PM
Thanks so much for the reply and help. The <br> that I want to remain is doing so. However, the \n newlines don't seem to be taking. That is, here's part of my output...
var point = new GLatLng(LAT,LON ); var marker = createMarker(point,"ID<br>STREET,CITY") mmap.addOverlay(marker); var point = new GLatLng(42.37,-122.9 ); var marker = createMarker(point,"JA119<br>STREET,CENTRAL POINT ") mmap.addOverlay(marker); var point = new GLatLng(43.4026,-124.262 ); var marker = createMarker(point,"CO29<br>STREET,COOS BAY ") mmap.addOverlay(marker); var point = new GLatLng(43.1038,-124.377 ); var marker = createMarker(point,"CO01<br>STREET,BANDON ") mmap.addOverlay(marker);

I'd like the output to all to be separated nicely like this but the \n doesn't seem to be doing its job:

var point = new GLatLng(43.4026,-124.262 );
var marker = createMarker(point,"CO29<br>STREET,COOS BAY ")
mmap.addOverlay(marker);

Can you clarify what I'm doing wrong? Thanks again for your time.

Fou-Lu
03-11-2010, 03:27 PM
What context are you using this in? From the looks of it you're wanting this to display in HTML (hense why it shows up on one line). The source itself has the newlines, I was under the impression you wanted it to actually process the JS itself.

In this case, you would convert the \n to <br />. Do so with an nl2br function:


$s = htmlspecialchars("var point = new GLatLng($latlon);\n
var marker = createMarker(point,\"$number<br>$address\")\n
mmap.addOverlay(marker);\n");
// $s now contains the 'source' type formatting. To print it as an html display in say a div:
printf("<div>%s</div>\n", nl2br($s));


Also, HTML has a tag itself to handle the \n, the <pre> tag. You can look into using this instead of using nl2br, but thats up to you. The nl2br function replaces '\n' with '<br />\n'. This should allow you to display what you want.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum