Multibyte trim in PHP?

后端 未结 8 1487
小鲜肉
小鲜肉 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 08:59

    mb_ereg_replace seems to get around that:

    function mb_trim($str,$regex = "(^\s+)|(\s+$)/us") {
        return mb_ereg_replace($regex, "", $str);
    }
    

    ..but I don't know enough about regular expressions to know how you'd then add on the "charlist" parameter people would expect to be able to feed to trim() - i.e. a list of characters to trim - so have just made the regex a parameter.

    It might be that you could have an array of special characters, then step through it for each character in the charlist and escape them accordingly when building the regex string.

提交回复
热议问题