I've got a string, that has some data returned form a form.

for example's sake:

The rabbit's going to have a higher # of children because they are known for reproducing at a rapid rate > that of humans, which spend years finding a mate.  And many times greater than ^Nerds^ who never seem to get laid, or ^geeks^ who... well, you know.
<br>
In summary, rabbits & geeks can coexist&nb sp; on this planet.

A few things here:
Note the > Also note the <br> Can't turn <br> into an html entity, or it won't be a html element anymore.

same with the & in "rabbits and geeks" should be &amp;

BUT...

&nb sp; should be left alone because it's already an entity.

So my question to the PHP programmers much wiser than me, is how do I turn necessary chracters into the appropriate HTML entities, without making mistakes?

There quite a few to deal with:
http://www.w3schools.com/html/html_entitiesref.asp

So what's the best way to htmlize a string? Without breaking HTML that a user might insert.

Edit; &nb sp; has a space to prevent this very issue 😉

    I could be wrong but it doesn't sound to me like you want anything translated.

    You want & left alone, &nb sp; left alone, and <br> left alone.

    I think the only thing you need to do is, if magicquotes are on, stripslashes...

     if (get_magic_quotes_gpc() == TRUE) {
      $data = stripslashes($data);
    }

      Not really.

      If someone writes

      Show & Tell

      & must be &nbsp; not just and.

      And there are dozens of other characters that must be expressed in html entities.

        Damnit this thing is useless. Lemme post a picture. Fortunately this bug is fixed in vB3.

        Well I guess you are going to have to htmlspecialchars and then
        to undo the damage on &nbsp: and to revert &lt: and &gt: to < and >, see attachment below:

          Write a Reply...