PHP:有没有办法看到像\n这样的“隐形”字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9345841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: is there a way to see "invisible" characters like \n
提问by lemon
Is there a way to see invisible characters like whitespace, newlines, and other non-printing characters in a manner like print_r() ?
有没有办法以 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.
看到 [0] 和 [1] 之间奇怪的空格了吗?当打印出 [0] 时,也会打印换行符。但是我没有在哪里为 [0] 分配换行符,所以我很困惑。
Is there a built in function in php that's like show_invisible(Object->numbers[0])
and it will show 123\n
or similar?
php 中是否有内置函数show_invisible(Object->numbers[0])
,它会显示123\n
或类似?
采纳答案by Flavius
You can use the addcslashesfunction:
您可以使用addcslashes函数:
string addcslashes ( string $str, string $charlist )
字符串 addcslashes ( 字符串 $str, 字符串 $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 [\]^_` ?>
<?php echo addcslashes('foo[ ]', 'A..z'); // output: \f\o\o\[ \] // All upper and lower-case letters will be escaped // ... but so will the [\]^_` ?>
回答by George Garchagudashvili
To see all the invisible characters not only \r
, \n
etc... It's good to see json_encode
ed version and everything is clear:
不仅要看到所有不可见的字符\r
,\n
等等...很高兴看到json_encode
ed 版本,一切都清楚了:
$str = "...";
echo json_encode($str);
回答by MyStream
You could probably list all the control characters out, but try this for a quick fix ?
你可能会列出所有的控制字符,但试试这个来快速修复?
PHP - print string with control characters
It's a simple str_replace("\n",'\n',$string)
kind of fix, but you could probably adapt the solution for a function callback on the array to convert those characters.
这是一种简单str_replace("\n",'\n',$string)
的修复,但您可能可以调整数组上的函数回调的解决方案以转换这些字符。
回答by Evert
You could just run your php script, and pipe it straight to hexdump -C
你可以运行你的 php 脚本,然后直接通过管道发送给 hexdump -C
回答by powtac
To have an exact replication of the input string, without the surrounding "
and without serialization, use this wrapper for json_encode()
:
要准确复制输入字符串,没有周围"
和序列化,请使用此包装器json_encode()
:
substr(json_encode((string)$string), 1, -1)
It does a string casting and removes the "
of the JSON standard.
它执行字符串转换并删除"
JSON 标准的 。