Skip to content

Instantly share code, notes, and snippets.

@MasterEx
Created September 29, 2011 18:57
Show Gist options
  • Save MasterEx/1251595 to your computer and use it in GitHub Desktop.
Save MasterEx/1251595 to your computer and use it in GitHub Desktop.
PHP function alters set of characters with the given one

This is a small function that removes the punctuation from a given string in Greek. I haven't added upper case letters or multitone letters (except one letter to show how it can be expanded to fit your needs).

This function is pretty useful when using uppercase functions in our templates, because having upper case letters with tones isn't pretty.

Besides that you can map any set of characters or strings with a given character/string and then use it to alter all the mapped elements with the given one.

In case you want to use it with string use the preg_replace function instead of str_replace.

<?php
function rmpun($string) {
$letters = array("α" => array("ά","ἀ"), "ε" => array("έ"),
"η" => array("ή"), "ι" => array("ί","ϊ"),
"ο" => array("ό"), "υ" => array("ύ","ϋ"),
"ω" => array("ώ")
);
$keys = array_keys($letters);
$i = 0;
foreach($letters as $letter) {
foreach($letter as $punletter) {
$string = str_replace($punletter,$keys[$i],$string);
}
$i++;
}
return $string;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="el" xml:lang="el">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php echo rmpun("Περικλής Ντανάσης και λέξεις με τόνους και φωνήεντα. Πολυτονικά: πῶς ἀπὸ"); ?>
<br/>
<?php echo mb_strtoupper(rmpun("Περικλής Ντανάσης και λέξεις με τόνους και φωνήεντα. Πολυτονικά: πῶς ἀπὸ"),"UTF-8"); ?>
</body>
</html>
Περικλης Ντανασης και λεξεις με τονους και φωνηεντα. Πολυτονικα: πῶς απὸ
ΠΕΡΙΚΛΗΣ ΝΤΑΝΑΣΗΣ ΚΑΙ ΛΕΞΕΙΣ ΜΕ ΤΟΝΟΥΣ ΚΑΙ ΦΩΝΗΕΝΤΑ. ΠΟΛΥΤΟΝΙΚΑ: ΠῶΣ ΑΠῸ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment