I am new to PHP and trying to display information from our database on our website. The data being displayed already has some html in it. For instance, the database will give something that looks like

Blah blah<br> blah

In order to get this to display properly when I use echo I used html_entity_decode. This got me the results I wanted when I was testing the webpage on our internal server (Mac Leopard Server). But when I put the page on the external web server that hosts our site it ceased to work. When I look at the html source of the returned web page it just shows

Blah blahbr blah

Our internal server that displays the information correctly is running php 5.2.4, the external web server has 5.2.6. Does anyone know why it would show differently on another server?

    I think you want to encode them for storage and decode them for display.

    There isn't a need to decode un-encoded HTML, such as your <br>.

      Generally speaking, having any need for html_entity_decode() seems rather strange in this circumstance. Are you running the inputs through htmlentities() before inserting into the database? If so, why? If not, then there should be no need for html_entity_decode(). In fact, even if you are, there's probably no need.

      So I guess we may need a bit more info as to what exactly is happening to the data before we can give you a good answer.

        When retrieving the data I use html_entity_decode and dump it straight into a variable. After that I echo the variable. When I try the same thing only without the decoding it looks exactly the same. The database my company uses is Filemaker Pro 9, so I am not sure what, if anything, is happening to the text on its way out. All of the Filemaker PHP files with the necessary commands for accessing the database where copied directly from our server. As such, there should not be any difference in the output right? I know it is probably not a database most PHP developers use a lot but no one on the Filemaker forums is bothering to respond to my question so I am hoping it is a PHP issue.
        This is the code I used both on our server and on the public server but with different results...

        $Detail = html_entity_decode($CurrentRecord->getField('WriteUpCalc'));
        echo $Detail;
        

          Let's look at what [man]html_entity_decode/man does:

          Convert all HTML entities to their applicable characters
          ...
          html_entity_decode() is the opposite of [man]htmlentities/man in that it converts all HTML entities to their applicable characters from string.

          So, unless for some reason you are applying htmlentities() (or possibly [man]htmlspecialchars/man) to the text when it is inserted into the database, there would seem to be no reason to apply html_entity_decode() to the text when extracting it from the database. And in my opinion there should probably not be any need to apply htmlentities() to the text when inserting it into the DB. (htmlentities() is for filtering text when outputting to the web browser if you want all characters that have HTML entity codes to be replaced with those codes; it has nothing to do with database SQL injection or any other reason to filter data for output to a database.)

          So ultimately here, I still lack any surety as to there being any need for html_entity_decode() here; and if there is a need, my preference would probably be to remove the cause of that need, rather than treating the symptom, so to speak.

            Thanks for the response NogDog. I certainly understand what you are saying and it makes sense. However, that doesn't explain the difference in the resulting html on the two servers. I have been doing things over so I can remind myself why I was using html_entity_decode. Hopefully this will make things more clear.

            Originally, when I was testing this page on our private server I did not change the information coming from the database at all. The result is that the <br> and similar statements show up in the html source as &lt;br&gt;, and thus displayed as <br> in the browser rather than as an actual line break. Since I do not know of a way to change how Filemaker outputs these characters I started using html_entity_decode. The result of which is a perfectly viewable page.

            When I move it to the other web server, with the html_entity_decode, and view the page source all of the <br> tags have changed to just br. When I remove the html_entity_decode the results do not change at all. The tags still show up without any carrots. This makes me think there is some weird setting on the public server. Does this make any sense to you?

              It does not make any sense to me. (Your writing makes sense, just why the server should matter does not.) However, I have zero experience with Filemaker so I do not have any idea what it may or may not do to your files in different environments (other than it sounds like a good reason for me not to use Filemaker 😉 ). At this point, though, it does not really sound like a PHP issue, unless somewhere along the line there is a str_replace(), preg_replace() or something similar that is explicitly changing the text.

                Write a Reply...