Home > Software design >  Convert/replace U 2009 character with space
Convert/replace U 2009 character with space

Time:07-06

I have a string that contains unreadable space character:

enter image description here

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 ?

CodePudding user response:

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)

  • Related