Samstag, 20. Februar 2016

Handling vowel mutation with php and databases

If you try to handle vowel mutation in combination with latin an cyrillic letters you it is very annoying to do this with functions like htmlentities or mb_convert_encoding.

For cyrillic letters those functions convert them into the corresponding ACSII-Code. So in databases you will see only plain latin letters and ACSII-Codes. But the result of converting vowel mutation looks strange.

The following exmaple shows how you convert every non latin letter or symbol into the corresponding ASCII-Code, so you can save it to the database and read it out by using html_entity_decode.

 
function convert_for_db($string){
    $for_db = "";
    for ($i = 0; $i < strlen($string); $i++){ 
 if(is_latin_char($string[$i])){
         $for_db .=  $string[$i];
 }
 else{
  $for_db .= to_ascii($string[$i]);
 }
    }
    return $for_db;
}
 
function to_ascii($char){
    $ascii = ord($char);
 return "&#".($ascii).";";
    }
 
function is_latin_char($char){
    $latin_chars = array("a","b","c","d","e","f","g","h","i",
                        "j","k","l","m","n","o","p", "q","r",
                        "s","t","u","v","w","x","y","z");
    return in_array(strtolower($char), $latin_chars);
}

Keine Kommentare:

Kommentar veröffentlichen