Given a string that contains control characters. The task is to remove all control characters from a string. In ASCII, all characters having a value below 32 come under this category. Some of the control characters are given below:
| S.NO | ASCII VALUE | Name | Symbol |
|---|---|---|---|
| 1. | 0 | null (NUL) | “\0” |
| 2. | 9 | horizontal tab(HT) | “\t” |
| 3. | 10 | line feed(LF) | “\n” |
| 4. | 11 | vertical tab(VT) | “\v” |
| 5. | 27 | escape(ESC) | “\e” |
Example:
Input: str = "\0\eGeeks\t\e\tfor\v\vGeeks" Output: GeeksforGeeks
In this article, we will remove control characters from the PHP string by using two different methods.
- Using General Regular Expression:
- Using ‘cntrl’ Regex:
Using General Regular Expression: There are many regular expressions that can be used to remove all Characters having ASCII value below 32. Here, we will be using preg_replace() method.
Note: The ereg_replace() method is removed from PHP >= 7, so here we will be using preg_replace() method.
- Program:
<?PHP// PHP program to remove all control// characters from string// String with control characters$str="\eGeeks\t\tfor\v\vGeeks\n";// Using preg_replace method to remove all// control characters from string$str= preg_replace('/[\x00-\x1F\x7F]/','',$str);// Display the modify stringecho($str);?>chevron_rightfilter_none - Output:
GeeksforGeeks
Using ‘cntrl’ Regex: This can also be used to remove all control characters. And [:cntrl:] stands for all control character.
- Program:
<?PHP// PHP program to remove all control// characters from string// String with control characters$str="\eGeeks\t\tfor\vGeeks\n";// Using preg_replace method to remove all// control characters from string$str= preg_replace('/[[:cntrl:]]/','',$str);// Display the modify stringecho($str);?>chevron_rightfilter_none - Output:
GeeksforGeeks
Recommended Posts:
- Numeric Control (NC) and Computer Numeric Control (CNC)
- How to remove all non-printable characters in a string in PHP?
- How to remove non-alphanumeric characters in PHP?
- How to remove all Non-ASCII characters from the string using JavaScript ?
- 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 all leading zeros in a string in PHP ?
- How to remove all white spaces from a string 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 | 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 ?
- Introduction of Control Unit and its Design
- Difference between Hardwired and Micro-programmed Control Unit | Set 2
- Difference between Horizontal and Vertical micro-programmed Control Unit
- Version Control Systems
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.

