Hi there guys,

I've got a form that I'm verifying. I want the user to be able to see the data they entered to check it. To do this, I'm trying to convert the return and newline tags to breaks to show up in the html page. Here's what I attempted to use, which I thought would work:

$convict_ex = strip_tags (strip_mq_gpc (substr ($_POST['convict_ex'], 0, 500)));
$convict_ex_br = str_replace ("<br>", "\r\n", mysql_real_escape_string ($convict_ex));

I thought this would solve my problem, but the newline tags are still showing up(no breaks).

Could someone tell me what I've done wrong?

thanks,
json

    I also tried "\r\n", thinking that I needed to escape the slashes, but had the same effect.

    line is still showing up as:

    My name is Bob.\r\n\r\nI am a convict.

    thanks,
    json

      First, the order of the parameters to str_replace() is "search for", "replace with", "string to modify"; so you need to swap the first two arguments. Also, the mysql_real_escape_string() is probably escaping those characters before you do your str_replace(). If you want to make it a bit more portable so that it will handle both unix-style ("\n") and Windows-style ("\r\n") line endings, you could do:

      $convict_ex_br = mysql_real_escape_string(preg_replace('/\r?\n/', '<br>', $convict_ex));
      

      Also note that there is a built-in [man]nl2br/man function. (I just wanted to preempt those who would recommend it, though I'm not crazy about it as it assumes XHTML format with no built-in option for HTML.)

        Thanks very much for your help guys. Running like a top now 🙂

        thanks,
        json

          NogDog wrote:

          it assumes XHTML format with no built-in option for HTML.

          Although the XHTML tag it produces is downward-compatible for HTML (and if you've got the choice, why wouldn't you use XHTML?🙂)

            Weedpacket wrote:

            Although the XHTML tag it produces is downward-compatible for HTML (and if you've got the choice, why wouldn't you use XHTML?🙂)

            Because XHTML provides no additional functionality over HTML in > 99.9% of the cases where it is used, probably almost as high a percentage of the time web servers end up serving it up as HTML rather than XML (XHTML is supposed to be a XML implementation of HTML, not a new version of HTML), and if you give it a .xml file suffix and do serve it up as XML, good ol' IE barfs on it.

            PS: I avoid using <br> tags as much as possible, so it's not that big of a deal to me, anyway. I prefer something like:

            printf("<p>%s</p>\n", preg_replace('/(\r?\n/)+', "</p>\n<p>", $text));
            
              NogDog wrote:

              XHTML is supposed to be a XML implementation of HTML, not a new version of HTML

              Yes; by greatly simplifying the syntax; makes it easier to generate and parse. It's noteworthy that the W3C describes XHTML as "the next generation of HTML" and the "successor of HTML")

              .xml file suffix and do serve it up as XML, good ol' IE barfs on it.

              So don't.
              http://www.w3.org/TR/xhtml1/#media

              XHTML Documents which follow the guidelines ... may be labeled with the Internet Media Type "text/html"....

              Of course, since even IE7 can't cope with a five-year-old technology, that's good enough reason to not use it at all. No-one uses it, so Microsoft doesn't support it. Microsoft doesn't support it, so no-one uses it. With an attitude like that we might as well give up using CSS.

                Write a Reply...