Kevin Duke Kevin Duke - 2 months ago 9
PHP Question

PHP: is there a way to see "invisible" characters like \n

Is there a way to see invisible characters like whitespace, newlines, and other non-printing characters in a manner like print_r() ?

Reason is there is some sort of character in my array that I can't see and breaking things.

Object Object
(
[name] => name
[numbers] => Array
(
[0] => 123

[1] => 456
[2] => 789
)
[action] => nothing
)


See the weird whitespace between [0] and [1]? When printing out [0] a newline gets printed as well. But no where do I assign a newline to [0] so I'm quite confused.

Is there a built in function in php that's like
show_invisible(Object->numbers[0])
and it will show
123\n
or similar?

Answer

You can use the addcslashes function:

string addcslashes ( string $str , string $charlist )

which will return a string with backslashes before characters. An example would be:

<?php
echo addcslashes('foo[ ]', 'A..z');
// output:  \f\o\o\[ \]
// All upper and lower-case letters will be escaped
// ... but so will the [\]^_`
?>