Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MINOR Fixed merge error from r112365

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112371 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information...
commit eb0de3cd7094e85a4a8bcf9da43ce9604d879da4 1 parent c5196c9
@chillu chillu authored
Showing with 56 additions and 0 deletions.
  1. +56 −0 core/model/Transliterator.php
View
56 core/model/Transliterator.php
@@ -0,0 +1,56 @@
+<?php
+
+/**
+ * Support class for converting unicode strings into a suitable 7-bit ASCII equivalent.
+ *
+ * Usage:
+ *
+ * $tr = new Transliterator();
+ * $ascii = $tr->toASCII($unicode);
+ */
+class Transliterator {
+ /**
+ * Allow the use of iconv() to perform transliteration. Set to false to disable.
+ * Even if this variable is true, iconv() won't be used if it's not installed.
+ */
+ static $use_iconv = false;
+
+ function __construct() {
+ // A constructor is necessary for Object::create() to work
+ }
+
+ /**
+ * Convert the given utf8 string to a safe ASCII source
+ */
+ function toASCII($source) {
+ if(function_exists('iconv') && self::$use_iconv) return $this->useIconv($source);
+ else return $this->useStrTr($source);
+ }
+
+ /**
+ * Transliteration using strtr() and a lookup table
+ */
+ protected function useStrTr($source) {
+ $table = array(
+ 'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
+ 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
+ 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
+ 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
+ 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'ae', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
+ 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
+ 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
+ 'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r',
+ 'Ā'=>'A', 'ā'=>'a', 'Ē'=>'E', 'ē'=>'e', 'Ī'=>'I', 'ī'=>'i', 'Ō'=>'O', 'ō'=>'o', 'Ū'=>'U', 'ū'=>'u',
+ 'œ'=>'oe', 'ß'=>'ss', 'ij'=>'ij',
+ );
+
+ return strtr($source, $table);
+ }
+
+ /**
+ * Transliteration using iconv()
+ */
+ protected function useIconv($source) {
+ return iconv("utf-8", "us-ascii//IGNORE//TRANSLIT", $source);
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.