您好我有这个功能用拉丁字符替换希腊字符
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("Το ελληνικό κείμενο εδώ");
我想知道是否有一种快速方法可以修改此功能,以便替换/删除$Name
中$greek
数组中''
不包含的任何字符{ p>
更新
我忘了提及在希腊,有一种打字方式主要用于年龄较小的人,如果他们用拉丁字符输入希腊语。例如,还没有标准的东西:
'ει'
'υ'
'ι'
'οι'
'η'
听起来像'e'
'θ'
可以写成'th'
或'8'
甚至'3'
因此,此转换需要自定义算法。
答案 0 :(得分:3)
我建议:
$regex = sprintf('/[^%s]/u', preg_quote(join($greek), '/'));
$string = preg_replace($regex, '', $string);
答案 1 :(得分:1)
您可能希望将其作为参数:
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);
答案 2 :(得分:1)
这可能会对您有所帮助:
$string = preg_replace("/[^\p{Greek}]+/u", '', $string);
,或正如HamZa所说:
$string = preg_replace("/\P{Greek}+/u", '', $string);