phptransliteration

str_replace remove any character not in array


Hello I have this function which replace's greek characters with latin characters

function greeklish($Name) 
{  
$greek   = array('α','ά','Ά','Α','β','Β','γ', 'Γ', 'δ','Δ','ε','έ','Ε','Έ','ζ','Ζ','η','ή','Η','θ','Θ','ι','ί','ϊ','ΐ','Ι','Ί', 'κ','Κ','λ','Λ','μ','Μ','ν','Ν','ξ','Ξ','ο','ό','Ο','Ό','π','Π','ρ','Ρ','σ','ς', 'Σ','τ','Τ','υ','ύ','Υ','Ύ','φ','Φ','χ','Χ','ψ','Ψ','ω','ώ','Ω','Ώ',' ',"'","'",','); 
$english = array('a', 'a','A','A','b','B','g','G','d','D','e','e','E','E','z','Z','i','i','I','th','Th', 'i','i','i','i','I','I','k','K','l','L','m','M','n','N','x','X','o','o','O','O','p','P' ,'r','R','s','s','S','t','T','u','u','Y','Y','f','F','ch','Ch','ps','Ps','o','o','O','O','_','_','_','_'); 
$string  = str_replace($greek, $english, $Name); 
return $string; 
} 
echo greeklish("Το ελληνικό κείμενο εδώ");

I was wondering if there is a quick way to modify this function in order to replace / remove any character found in $Name that is not in $greek array with ''

UPDATE

I forgot to mention that in Greece there is a way of typing (mostly used among people of younger age) were they type Greek words using Latin characters. Also there is not something standard for example:

'ει' 'υ' 'ι' 'οι' 'η' sound like 'e'

'θ' can be written like 'th' or '8' or even '3'

so a custom algorithm is needed for this conversion.


Solution

  • I'd suggest this:

    $regex  = sprintf('/[^%s]/u', preg_quote(join($greek), '/'));
    $string = preg_replace($regex, '', $string);