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.
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.
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”.
Geeks for Geeks
- How to remove control characters from PHP string ?
- 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
- 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 ?
- 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 email@example.com. 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.