Rooster
Rooster

Reputation: 10077

How can I see '\r\n' in my var_dump or print_r?

Im trying to debug where the problem is in this dynamic input generator tool that another dev made. He uses '\r\n' to basically explode values to be stored as options in a select. Im trying to work out where this isnt being done because when the select is rendered the string is coming out of the database as one long string. I ran it through a nl2br to verify as well. SO I've determined the problem is when the data is being saved to the database on the edit command as it seems to be working on the create. But thats somewhat besides the point....

My question is thus: is there a simple way to have a var_dump or print_r function include '\r\n' s in their output?

All i can really think to do is replace these characters with something else before outputting which is kind of a pain so it would be awesome if theres an easier way.

Upvotes: 0

Views: 3090

Answers (2)

tadeuzagallo
tadeuzagallo

Reputation: 2522

Double the backslashes with preg_replace

<?php var_dump(preg_replace(array('/\n/','/\t/','/\s/'), array('\\\\n', '\\\\t', '\\\\s'), "\n\n"));

You can write a function to don't need to repeat this code all the time, I also like to put my var_dump's inside <pre> tags.

Upvotes: 1

SeanCannon
SeanCannon

Reputation: 78046

Put it in single quotes if you want to capture the string \r\n.

This:

echo "Here is line one \r\n Here is line two";

Renders this:

Here is line one
Here is line two

Whereas this:

echo 'Here is line one \r\n still on line one';

Renders this:

Here is line one \r\n still on line one

Upvotes: 0

Related Questions