Multibyte trim in PHP?

后端 未结 8 1562
小鲜肉
小鲜肉 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:01

    This version supports the second optional parameter $charlist:

    function mb_trim ($string, $charlist = null) 
    {   
        if (is_null($charlist)) {
            return trim ($string);
        } 
    
        $charlist = str_replace ('/', '\/', preg_quote ($charlist));
        return preg_replace ("/(^[$charlist]+)|([$charlist]+$)/us", '', $string);
    }
    

    Does not support ".." for ranges though.

提交回复
热议问题