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

In PHP, you can make invisible characters visible using the addcslashes() function, which adds backslashes before specified characters to reveal hidden whitespace, control characters, and other non-printing characters.

Syntax

string addcslashes(string $str, string $charlist)

Parameters:

  • $str − The string to be escaped
  • $charlist − Characters to escape (can use ranges like A..z)

Example

Here's how to reveal invisible characters in a string ?

<?php
    // String with invisible characters (space and tabs)
    $text = "sample[ ]";
    
    // Make characters visible by escaping A-z range
    echo addcslashes($text, 'A..z');
    echo "<br>";
    
    // Another example with tabs and newlines
    $hidden = "text\twith\nhidden\r\nchars";
    echo addcslashes($hidden, "\t<br>\r");
?>

The output of the above code is ?

\s\a\m\p\l\e\[ \]
text\	with\
hidden\
\
chars

Common Use Cases

This technique is useful for debugging strings, detecting unwanted whitespace, or preparing strings for specific output formats where invisible characters need to be visible.

Conclusion

The addcslashes() function provides an effective way to visualize invisible characters by escaping them with backslashes, making debugging and string analysis much easier.

Updated on: 2026-03-15T08:33:23+05:30

486 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements