How to transform a string to lowercase with preg_r

2019-06-21 01:58发布

I just stuck at this and cannot find solution. I would like to try to transform a string to lower case using preg_replace. I just cannot create the right regex. The reason is that normal strtolower does not support unicode characters. I know that I could use mb_strtolower but this function seems to be quite slow and beside them not everyone has MB support.

Any clue?

Regards, Radek

EDIT: Ok, thanks alot for your help guys. I think my approach was not quite correct. I think it would be much better to use this: How do I detect non-ASCII characters in a string? and then respectively use either the strtolower or mb_strtolower if available.

2条回答
贪生不怕死
2楼-- · 2019-06-21 02:12

Regex is not able to change characters by itself, it can only change their order and/or add additional characters/delete some of them.

There is preg_replace_callback or /e flag, but they can manipulate only with known functions, and therefore can't do better than strtolower.

If you can't rely on existense of mb_strolower function, you will have to implement it yourself.

查看更多
来,给爷笑一个
3楼-- · 2019-06-21 02:34

You shouldn't use a preg_replace for this because preg_replace is used to match a certain pattern and replace it with something else. Wat you want is to replace every single uppercase character with a lowercase one, so no need to match a pattern.

mb_strtolower would be the way to go, and if you don't have the mb_ functions you'll have to write a function yourself using a lot of str_replace's...

查看更多
登录 后发表回答