How to convert string to lowercase using preg_replace

I am just stuck in this and cannot find a solution. I would like to try converting a string to lowercase using preg_replace . I just can't create the correct regular expression. The reason is that regular strtolower does not support Unicode characters. I know that I could use mb_strtolower , but this function seems rather slow, and not everyone has MB support next to them.

Any clue?

Regards, Radek

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

+6
source share
2 answers

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

There is a preg_replace_callback or / e flag, but they can only manipulate well-known functions and therefore cannot be better than strtolower.

If you cannot rely on the existence of the mb_strolower function, you will have to implement it yourself.

+5
source

You should not use preg_replace for this, because preg_replace is used to match a specific pattern and replaces it with something else. The wat you want is to replace each uppercase character with lowercase, so there is no need to match the pattern.

mb_strtolower is the way to go, and if you don't have mb_ functions, you will need to write the function yourself using a lot of str_replace ...

0
source

Source: https://habr.com/ru/post/912026/


All Articles