$text = preg_replace('~\b(\w)(\w+)~e', '"$1".strtolower("$2")', $text);

I was using this function to convert uppercase characters to lowercase (except first character of each word).

It was working fine but now I moved to a new server with the last Php version and this function does not work correctly anymore.

The problem is that it converts all non-english characters into ������

In an older Php version this function just ignored non-english characters and did not replace anything.

Any ideas how to fix it (to force to replace non-english characters or to ignore them)?

Thanks.

    Aren't you essentially duplicating the functionality of [man]ucwords/man?

    As for your issue, try adding the 'u' modifier to your pattern (see man page: [man]reference.pcre.pattern.modifiers[/man]).

    EDIT: Rather than ucwords(), I suppose [man]mb_convert_case/man might be more appropriate since you're dealing with "non-english characters."

      Yes, I know mb_convert_case() but I need a function that does not touch the first character of each word. It's not possible with mb_convert_case() .

        lpa wrote:
        Yes, I know mb_convert_case() but I need a function that does not touch the first character of each word. It's not possible with mb_convert_case() . 

        Isn't that the idea behind MB_CASE_TITLE?

          No, Weedpacket. MB_CASE_TITLE makes first character of each word Uppercase. I just want a function that doesn't touch the first character of each word.

            Well in that case, see bradgrafelman's suggestion; note also that (a) [man]preg_match[/man] can use Unicode character properties (such as being able to distinguish between lowercase, uppercase, and titlecase), and (b), the [font=monospace]/e[/font] modifier is deprecated in favour of [man]preg_replace_callback[/man].

            You may also still want to use mb_convert_case as a replacement for strtolower.

              I tried it with mb_convert_case and mb_strtolower but can´t get it working. I will remove it from the script, howewer I hate users who always use UPPERCASE when posting comments. Thanks.

                If you still want to try using your regular expression based approach, note your pattern was only missing one letter (as suggested in my first reply above).

                  7 days later

                  Maybe I am doing something wrong. I tried to add "u" but it still does not work. 🙁

                    Can you show us the code you tried (including example text)?

                      Write a Reply...