I have a string that contains unreadable space character:
How can I replace this character with a normal space so I can get a string like: "a b c d"
I have tried this:
$str = utf8_decode($str);
But it converts that character to question mark ?
>Solution :
Try to replace using preg match –
$string = " test string and XY \t ";
$trimString = trim(preg_replace('/[\pZ\pC]/u', ' ', $string));
//test\x20\x20\x20string\x20and\x20XY
Details
-
^[\pZ\pC]+ – one or more whitespace or control chars at the start of string
-
| – or
[\pZ\pC]+$ – one or more whitespace or control chars at the end of string -
| – or
(?! )[\pZ\pC] – one or more whitespace or control chars other than a regular space anywhere inside the string -
[^\S ] – any whitespace other than a regular space (\x20)
