PHP remove non printable characters from a string

Regex to remove non printable characters from a string

Space is first printable char and tilde (~) is last printable ascii char. We replace any char which does not fall in that range.

<?php
$str = "Hello\n\tWorld";
$newstr = preg_replace('/[^ -~]/', "", $str);
var_dump($str);
var_dump($newstr);
?>
Env: PHP 5.5.9 (Linux)

Regex to replace non printable characters and space and collapse multiple spaces

Replace multiple occurences of chars which donot not fall in range (! to ~) with one space. Regex by default is greedy.

<?php
$str = "Hello\n\tWorld";
$newstr = preg_replace('/[^!-~]+/', " ", $str);
var_dump($str);
var_dump($newstr);
?>
Env: PHP 5.5.9 (Linux)

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments