How to remove all non-printable characters in a string in PHP? Last Updated : 03 May, 2023 Comments Improve Suggest changes Like Article Like Report 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 different 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 // 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); ?> Output: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 // 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); ?> Output:Geeks for Geeks Comment More infoAdvertise with us Next Article How to Iterate Over Characters of a String in PHP ? 29AjayKumar Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads How to remove portion of a string after a certain character in PHP? In PHP, removing a portion of a string after a certain character refers to trimming the string by locating a specific character and then eliminating everything that follows it. This technique is useful for extracting substrings or cleaning data by removing unwanted parts.Here we are some common appr 2 min read How to remove portion of a string after a certain character in PHP? In PHP, removing a portion of a string after a certain character refers to trimming the string by locating a specific character and then eliminating everything that follows it. This technique is useful for extracting substrings or cleaning data by removing unwanted parts.Here we are some common appr 2 min read How to remove portion of a string after a certain character in PHP? In PHP, removing a portion of a string after a certain character refers to trimming the string by locating a specific character and then eliminating everything that follows it. This technique is useful for extracting substrings or cleaning data by removing unwanted parts.Here we are some common appr 2 min read How to find number of characters in a string in PHP ? We have given a string and the task is to count number of characters in a string str in PHP. In order to do this task, we have the following methods in PHP:Table of ContentMethod 1: Using strlen() MethodMethod 2: Using mb_strlen() MethodMethod 3: Using iconv_strlen() MethodMethod 4: Using grapheme_s 3 min read How to Iterate Over Characters of a String in PHP ? This article will show you how to iterate over characters of string in PHP. It means how to loop through an array of characters in a string. There are two methods to iterate over the character of a string, these are:Table of ContentUsing str_split() function and foreach LoopUsing for LoopUsing mb_su 3 min read How to remove control characters from PHP string ? 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 ta 2 min read Like