php-regexHow to remove non-printable characters using PHP regex?
Using PHP regex, non-printable characters can be removed from a string. The following example code block uses the preg_replace
function to remove all non-printable characters from a string:
$string = preg_replace('/[\x00-\x1F\x7F]/u', '', $string);
This code will output a string with all non-printable characters removed:
This is a string with non-printable characters removed.
The code consists of the following parts:
preg_replace
: This is a PHP function used to perform a regular expression search and replace./[\x00-\x1F\x7F]/u
: This is the regular expression used to match all non-printable characters.$string
: This is the string to be searched and replaced.
Helpful links
More of Php Regex
- How to match a double quote in PHP regex?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to match a space using PHP regex?
- How to match a single quote in PHP regex?
- How to use named capture groups in PHP regex?
- How to match the end of a string when using regex in PHP?
- How to use an "or" condition in PHP regex?
- How to get last matched occurrence in PHP regex?
- How to use PHP regex to match a zip code?
- How to use PHP regex with zero or more occurrences?
See more codes...