Need your help: Change strange characters to US characters

Discussion in 'PHP' started by lovelycesar, Aug 21, 2008.

  1. #1
    I want to do a function for converting some strange characters into US characters as below:
    - if the character is one of: "ó, ò, ỏ, õ, ọ, ô, ố, ồ, ổ, ỗ, ộ, ơ, ớ, ờ, ở, ỡ, ợ", then the function will change to "o".
    - if the character is one of: "á, à, ả, ã, ạ, ă, ắ, ằ, ẳ, ẵ, ặ, â, ấ, ầ, ẩ, ẫ, ậ", then the function will change to "a".
    - if the character is one of: "é, è, ẻ, ẽ, ẹ, ê, ế, ề, ể, ễ, ệ", then the function will change to "e".
    - if the character is one of: "ú, ù, ủ, ũ, ụ, ư, ứ, ừ, ử, ữ, ự", then the function will change to "u".
    - if the character is one of: "í, ì, ỉ, ĩ, ị", then the function will change to "i".

    I tried to write this function, but the input is encoded in UTF-8 unicode, if I use HTML codes of these characters, it cannot convert.

    Thank you for your help.
     
    lovelycesar, Aug 21, 2008 IP
  2. nice.wallpapers

    nice.wallpapers Active Member

    Messages:
    142
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Hi You can do it like this

    <?
    $what_to_replace = array("/ó/","/á/","/é/");
    
    $replace_with = array("o","a","e"); 
    
    print preg_replace($what_to_replace, $replace_with, "ó123é456á");
    // Out put will be like this o123e456a
    ?>
    PHP:
    Thanks :)
     
    nice.wallpapers, Aug 21, 2008 IP
  3. lovelycesar

    lovelycesar Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I tried this method, but these characters cannot be typed in textfield of notepad (editor for my scripts)
     
    lovelycesar, Aug 21, 2008 IP
  4. nice.wallpapers

    nice.wallpapers Active Member

    Messages:
    142
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Hi it is working fine on Dreamweaver and also on vi editor on linux.

    Thanks,
     
    nice.wallpapers, Aug 21, 2008 IP
    lovelycesar likes this.
  5. lovelycesar

    lovelycesar Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank bro!
    To have these characters I must use an external tool for typing. It doesn't work well on Linux, and I don't have a Dreamwaver, what should I do?
     
    lovelycesar, Aug 21, 2008 IP
  6. nice.wallpapers

    nice.wallpapers Active Member

    Messages:
    142
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Hi,

    Can you tell me one more thing , those values 'ó' i mean this kind of characters are stored in database ? if yes then you can also remove them by sql queries.

    thanks
     
    nice.wallpapers, Aug 21, 2008 IP
  7. lovelycesar

    lovelycesar Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thank you for your reply.
    I'm writing a submit form for a post of contents, and want to rewrite URLs, for example, the user will fill out the TITLE of the post in these chararters, the form will assign an URL rewriting automatically, but in US ASCII characters.
     
    lovelycesar, Aug 22, 2008 IP
  8. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #8
    hey, if you are unable to use the actual character, try using there ascii code then use chr() to generate the character in a str_replace method...I can help if you pm me.
     
    NatalicWolf, Aug 22, 2008 IP
  9. lovelycesar

    lovelycesar Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hi, thank for you guide. But the method using chr() is a bit inapplicable. For example, to type "ô" in the form, the user must type "o" two times or "o" with number 5.

    Is it a hard problem to be resolved?
     
    lovelycesar, Aug 23, 2008 IP
  10. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Part of your problem is, some of the symbols are unicode, PHP doesn't actively support unicode yet...Well php 6 does but not 4 or 5. I think you can replace some of the characters...but not sure about the rest.
     
    NatalicWolf, Aug 23, 2008 IP
  11. lovelycesar

    lovelycesar Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Yes, exactly. I did it, but cannot replace all these chars.
     
    lovelycesar, Aug 23, 2008 IP
  12. lovelycesar

    lovelycesar Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Eureka! I resolved it !
    As following,
    - In the submit form, I use encode charset UTF-8
    - step 2:
    
    	$src = urlencode($src);
    	$src = preg_replace('/\%/', '', $src);
    	$src = preg_replace('/C3A1/', 'a', $src); // "á" value has been changed to a
    	$src = preg_replace('/C3A0/', 'a', $src); // "à" value has been changed to a
    	$src = preg_replace('/E1BAA3/', 'a', $src); // "ả" value has been changed to a
    	$src = preg_replace('/C3A3/', 'a', $src); // "ã" value has been changed to a
    	$src = preg_replace('/E1BAA1/', 'a', $src); // "ạ" value has been changed to a
    
    Code (php):
    - step 3: By the way, thank all pro bros.
     
    lovelycesar, Aug 23, 2008 IP