Given a string which contains printable and not-printable characters. The task is to remove all non-printable characters from the string. Space ( ) is first printable char and tilde (~) is last printable ASCII characters. So the task is to replace all characters which do fall in that range means to take only those char which occur in range(32-127). This task is done by only differents type regex expression.
Example:
Input: str = "\n\nGeeks \n\n\n\tfor Geeks\n\t" Output: Geeks for Geeks
Note: Newline (\n) and tab (\t) are commands not printable character.
Method 1: Using general regular expression: There are many regex available. The best solution is to strip all non-ASCII characters from the input string, that can be done with this preg_replace.
Example:
<?PHP // PHP program to remove all non-printable // character from string // String with non printable characters $str = "Geeks šžfor ÂGee\tks\n"; // Using preg_replace method to remove all // non-printable character from string $str = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $str); // Display the modify string echo($str); ?> |
Geeks for Geeks
Method 2: Use the ‘print’ regex: Other possible solution is to use the print regular expression. The [:print:] regular expression stands for “any printable character”.
Example:
<?PHP // PHP program to remove all non-printable // character from string // String with non printable char $str = "Geeks šžfor ÂGee\tks\n"; // Using preg_replace method to remove all // non-printable character from string $str = preg_replace('/[[:^print:]]/', '', $str); // Display modify string echo($str); ?> |
Geeks for Geeks
Recommended Posts:
- How to remove control characters from PHP string ?
- How to remove all Non-ASCII characters from the string using JavaScript ?
- How to remove non-alphanumeric characters in PHP?
- How to remove all leading zeros in a string in PHP ?
- How to remove all white spaces from a string in PHP ?
- How to remove the first character of string in PHP?
- Remove new lines from string in PHP
- How to remove extension from string in PHP?
- How to remove line breaks from the string in PHP?
- How to remove portion of a string after a certain character in PHP?
- How to Remove Special Character from String in PHP ?
- How to remove white spaces only beginning/end of a string using PHP ?
- PHP | Check if all characters are lower case
- How to remove all line breaks from a string using JavaScript?
- How to remove all classes that begin with a certain string in JavaScript?
- PHP | Different characters in the given string
- How to get the last n characters of a PHP string?
- How to find number of characters in a string in PHP ?
- JavaScript | Strip all non-numeric characters from string
- PHP | Remove duplicate elements from Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

