To remove all Unicode whitespace with control chars at the start and end of string, and remove all Unicode whitespace with control chars other than regular space anywhere inside the string, you can use
preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$|(?! )[\pZ\pC]/u', '', $string)
// Or, simply
preg_replace('/^\s+|\s+$|[^\S ]/u', '', $string)
See the regex demo #1 and regex demo #2.
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
)
If you need to "exclude" common line break chars, too, replace (?! )[\pZ\pC]
with (?![ \r\n])[\pZ\pC]
(as suggested by @MonkeyZeus), in the second regex, it means you need to use [^\S \r\n]
.
See PHP demo:
echo preg_replace('~^[\pZ\pC]+|[\pZ\pC]+$|(?! )[\pZ\pC]~u', '', 'abc def ghi ');
// => abc defghi
echo preg_replace('/^\s+|\s+$|[^\S ]/u', '', 'abc def ghi ');
// => abc defghi