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);
?>string(12) "Hello World" string(10) "HelloWorld"
Env: PHP 8.2.29 (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);
?>string(12) "Hello World" string(11) "Hello World"
Env: PHP 8.2.29 (Linux)