Multibyte trim in PHP?

后端 未结 8 1542
小鲜肉
小鲜肉 2020-11-28 08:19

Apparently there\'s no mb_trim in the mb_* family, so I\'m trying to implement one for my own.

I recently found this regex in a comment in php.net:

8条回答
  •  孤城傲影
    2020-11-28 09:08

    You can also trim non-ascii compatible spaces (non-breaking space for example) on UTF-8 strings with preg_replace('/^\p{Z}+|\p{Z}+$/u','',$str);

    \s will only match "ascii compatible" space character even with the u modifier.
    but \p{Z} will match all known unicode space characters

提交回复
热议问题