简体   繁体   中英

Trim, lower case, upper case words and save in MongoDb

I'm trying to save a user provided string into MongoDb, but doing this before saving to normalize:

ucwords(mb_strtolower(trim($name)))

But that makes MongoDb throw an MongoException with characters like "æøå".

I thought "mb_" made the function work properly with UTF-8? Using plain strtolower fixes the problem.

Thanks

If you are on PHP 5.4 the default charset has changed from from ISO-8859-1 to UTF-8.

strtolower should do a perfect job for you, unless your encoding from $name is not ISO-8859-1 or UTF-8

MongoDB always expects UTF-8 strings.

As @JvdBerg says it depends on your PHP but default before PHP 5.4 isn't UTF-8 so you would need to use either iconv or utf8_encode to "encode" the string. It is most likely you are using an old PHP and the string is actually ISO encoded instead.

mb_ functions merely take multibyte characters into account when it does it's transformations it doesn't encode the string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM