Domanda

Hello I'm haveing this function to replace 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("Το ελληνικό κείμενο εδώ");

and 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 taht in greece there is a way of typing mostly used among people of younger age in 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.

È stato utile?

Soluzione

I'd suggest this:

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

Altri suggerimenti

You may want to have it as a parameter:

function greeklish($Name, $invert=false) 
{  
   $greek   = array('α','ά','Ά','Α','β','Β','γ', 'Γ', 'δ','Δ','ε','έ','Ε','Έ','ζ','Ζ','η','ή','Η','θ','Θ','ι','ί','ϊ','ΐ','Ι','Ί', 'κ','Κ','λ','Λ','μ','Μ','ν','Ν','ξ','Ξ','ο','ό','Ο','Ό','π','Π','ρ','Ρ','σ','ς', 'Σ','τ','Τ','υ','ύ','Υ','Ύ','φ','Φ','χ','Χ','ψ','Ψ','ω','ώ','Ω','Ώ',' ',"'","'",','); 
   if($invert)
   {
      return preg_replace('/[^'.preg_quote(join('', $greek), '/').']/', '', $Name);
   }
   $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("Το ελληνικό κείμενο εδώ", 1);

This might help you:

$string = preg_replace("/[^\p{Greek}]+/u", '', $string);

, or as HamZa mentioned:

$string = preg_replace("/\P{Greek}+/u", '', $string);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top