Hi,
when I use print_r to print an array, I don't get a nice human display. only if I do view source I get the human readable display.

How can I do it so It will be displayed well .

(I'm familier with the ob_start/ob_get_contents functions that are needed here, I just need the fnction that translates the space characters and \n to html)

    Use nl2br and str_replace to convert these characters.

    Thomas

      I just put it in <pre> tags. I have a function like this:

      function print_var($var) {
      echo "\n<hr />\n\n";
      print_r($var);
      echo "\n\n<hr />\n<br />\n";
      }

      It's made to look clear both on screen and in the source code.

        Quboid,
        I don't see why having a horizontal line as <hr /> does will help me? anyway, I tried it and it didn't.

        what I finally did was:

        ob_start();
        print_r($arr);
        $str = ob_get_contents();
        ob_end_clean();
        $str = preg_replace("/ /","&nbsp;",$str);
        $str = nl2br($str);
        print_r($str);

        now it looks good on html, but in the source code it is a big mess of nbsp. its less important to me, but if anyone has suggestion to make it good both in html and in source code, I'd be happy to read.

        Thanks
        ravid

          Hi,

          I think Quboid meant something like

          function print_var($var) {
          echo "\n<pre>\n";
          print_r($var);
          echo "</pre>\n<br />\n";
          }
          
          print_var($arr);
          

          Thomas

            (Quboid did say "<pre>" tags...)

            Instead of <pre>, and since this is a style issue rather than a content issue, CSS would be the appropriate choice here.

            <div style="white-space:pre;">
            foo
            bar
                badger
            </div>

            The big problem with this approach is - as is usual when it comes to CSS - brain-dead IE. It doesn't recognise the "white-space" property. I'd probably stick with <pre>: a monospace font is to be expected in this scenario anyway (that's why &#91;code] and &#91;php] blocks use a monospace font; it makes the code easier to read).

              I just put <hr>'s in to help me see the start and end of the deubgging print.

                Write a Reply...