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 use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match special characters?
- How to match a space using PHP regex?
- How to use PHP regex to match an exact string?
- How to use PHP regex to match whitespace?
- How to use PHP regex to match UTF8?
- How to use an "or" condition in PHP regex?
- How to use PHP regex to match an XML tag?
- How to use PHP regex to match URL?
- How to use PHP regex to match a hashtag?
See more codes...