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

后端 未结 5 1544
北荒
北荒 2020-12-14 01:52

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

相关标签:
5条回答
  • To see all the invisible characters not only \r, \n etc... It's good to see json_encodeed version and everything is clear:

    $str = "...";
    echo json_encode($str);
    
    0 讨论(0)
  • 2020-12-14 02:12

    To have an exact replication of the input string, without the surrounding " and without serialization, use this wrapper for json_encode():

    substr(json_encode((string)$string), 1, -1)
    

    It does a string casting and removes the " of the JSON standard.

    0 讨论(0)
  • 2020-12-14 02:20

    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.

    0 讨论(0)
  • 2020-12-14 02:21

    You could just run your php script, and pipe it straight to hexdump -C

    0 讨论(0)
  • 2020-12-14 02:22

    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 [\]^_`
    ?>
    
    0 讨论(0)
提交回复
热议问题