preg_replace function does not work correctly with non-english characters in last php
Results 1 to 10 of 10

Thread: preg_replace function does not work correctly with non-english characters in last php

  1. #1
    Senior Member
    Join Date
    Apr 2009
    Location
    Gran Canaria
    Posts
    103

    preg_replace function does not work correctly with non-english characters in last php

    PHP Code:
    $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.

  2. #2
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,410
    Aren't you essentially duplicating the functionality of ucwords()?

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

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

  3. #3
    Senior Member
    Join Date
    Apr 2009
    Location
    Gran Canaria
    Posts
    103
    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() .

  4. #4
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,855
    Quote Originally Posted by lpa

    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?
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  5. #5
    Senior Member
    Join Date
    Apr 2009
    Location
    Gran Canaria
    Posts
    103
    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.

  6. #6
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,855
    Well in that case, see bradgrafelman's suggestion; note also that (a) preg_match can use Unicode character properties (such as being able to distinguish between lowercase, uppercase, and titlecase), and (b), the /e modifier is deprecated in favour of preg_replace_callback.

    You may also still want to use mb_convert_case as a replacement for strtolower.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  7. #7
    Senior Member
    Join Date
    Apr 2009
    Location
    Gran Canaria
    Posts
    103
    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.

  8. #8
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,410
    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).

  9. #9
    Senior Member
    Join Date
    Apr 2009
    Location
    Gran Canaria
    Posts
    103
    Maybe I am doing something wrong. I tried to add "u" but it still does not work.

  10. #10
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,410
    Can you show us the code you tried (including example text)?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •