Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    May 2007
    Posts
    162
    Thanks
    13
    Thanked 0 Times in 0 Posts

    echo raw html with php

    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:
    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!

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,446
    Thanks
    13
    Thanked 361 Times in 357 Posts
    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.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    <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.

    PHP Code:
    echo htmlspecialchars("var point = new GLatLng($latlon);\n
         var marker = createMarker(point,\"$number<br>$address\")\n
         mmap.addOverlay(marker);\n"
    ); 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    Regular Coder
    Join Date
    May 2007
    Posts
    162
    Thanks
    13
    Thanked 0 Times in 0 Posts
    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.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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:
    PHP Code:
    $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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •