Jump to content

"\n" not working, arrays not displaying properly...


Recommended Posts

Hey everyone,

 

PHP is not co-operating today!

I'm having issues creating new lines...

 

This is my code:

 

<?php
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
print_r ($a);
echo "first line". "\n";
echo "second line\n";
echo "third line \n";
echo "...";
?>

 

The expected output is:

 

Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )
)
first line
second line
third line
...

 

The actual output is:

 

Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z ) ) first line second line third line ...

 

Which is hardly readable when using large arrays!

Any help us much appreciated  :D

 

Thanks in advance!

 

 

 

Link to comment
Share on other sites

If you view the source of the output you'll see it is as expected. You're looking for HTML <br /> line breaks. If you want the print_r to display the line breaks in html as well, use the second parameter in print_r to return the output and then perform nl2br on that to convert character \n to <br />.

 

echo nl2br(print_r($a, true));

Link to comment
Share on other sites

You need to read up on content types. If you send data as text/html to a web browser, it'll regard it as HTML. In HTML, consecutive whitespace is ignored. For example, a billion space characters would still only be represented as a single space.

Link to comment
Share on other sites

As Daniel said, send the output as plain text rather than HTML and you won't have to play around with writing HTML tags to show the text in a monospace font.  Use something like the following, before your print_r:

 

header('Content-Type: text/plain; charset=utf-8');

 

 

Edit: Can't speel correctely!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.