Non text control characters can cause problems in content, especially when attempting to act upon that content.
To remove these control characters from a string use the following.
$value = preg_replace('/[^\PC\s]/u', '', $value);
The "\P" means that we want to look for unicode characters, and adding the "C" means that we want to find invisible control characters and unused code points. This is equivalent to "\p{C}".
You can also use this variant.
$value = preg_replace('/[[:cntrl:]]/u', '', $value);
The "[:cntrl:]" here denotes control characters in the text, which we then remove.
Add new comment