0

I was wondering is there is a script to Englishfy foreing characters in plain ascii characters without any accents. The reason I want this is because I have a web-app that dynamically creates databases, based on a given submitted company-name.

So far I have been able to build this code which seems to work well but only for English, Slavic and Greek characters. Ideally I want to have it for all languages, but it seems tedious to go through all characters (including accents) to achieve this.

class englishfy
{
    public $array = array();

    public function __construct()
    {
        $this->add_str("<empty>","ьъ");
        $this->add_str("a","аαάáąäăâăàā"); $this->add_str("ae","æ"); $this->add_str("b","бβb");$this->add_str("c","ćčç");
        $this->add_str("ch","чхχ"); $this->add_str("d","дδđ"); $this->add_str("e","еэεєęěéêëèēέ"); $this->add_str("f","фφ");
        $this->add_str("g","гγґğ"); $this->add_str("i","иыіηήιϊίíîïìī"); $this->add_str("k","кκķ"); $this->add_str("l","лλ");
        $this->add_str("m","мμ"); $this->add_str("n","нνňñ"); $this->add_str("ny","ń"); $this->add_str("o","оοωώόóöôòõø");
        $this->add_str("p","пπ"); $this->add_str("ps","ψ"); $this->add_str("r","рρř"); $this->add_str("s","сσςšşșś");
        $this->add_str("sh","шщ"); $this->add_str("ss","ß"); $this->add_str("sz","ж"); $this->add_str("t","тτț");
        $this->add_str("th","θ"); $this->add_str("ts","ц"); $this->add_str("u","уυύúůüùûū"); $this->add_str("v","в");
        $this->add_str("w","ł"); $this->add_str("x","ξ"); $this->add_str("y","йýÿý"); $this->add_str("ya","я");
        $this->add_str("ye","ї");$this->add_str("yo","ё");$this->add_str("yu","ю"); $this->add_str("z","зζżźž");
    }

    private function add_str($string,$chars)
    {
        $this->array[$string]=array();
        $this->array[mb_strtoupper($string)]=array();
        $chars=mb_strtolower($chars);

        $l=mb_strlen($chars);
        for($n=0;$n<$l;$n++)
        {
            $letter = mb_substr($chars,$n,1);
            if(!in_array($letter,$this->array[$string]) and $letter !== $string)
            {
                array_push($this->array[$string],$letter);
                array_push($this->array[mb_strtoupper($string)],mb_strtoupper($letter));
            }
        }   
    }

    public function return_english($string)
    {
        foreach($this->array as $key => $value){if (is_array($value)){$string = str_replace($value,$key,$string);}}
        return str_replace("<empty>","",$string);
    }
}
//#########################################
$obj = new englishfy();
echo $obj->return_english("Hello, Привет, Köszönni, Γειά σου, cześć ");//returns Hello, Privet, Koszonni, Geia sou, czesc
Patrick
  • 383
  • 1
  • 2
  • 19
  • 4
    You shouldn't change a company's name just to fit in your arbitrary restriction of "the English Language". Let's say I'm a UK-based car dealer that's quite specialist, and I'm called "The Coupé Warehouse". Am I not allowed to have that name? – Niet the Dark Absol Apr 18 '16 at 11:57
  • Of course,but I would like the (mysql) database name then to be ' thecoupewarehouse'. I noticed when I created database names in utf8 I got sql errors – Patrick Apr 18 '16 at 11:59
  • To clarify: I want users to login using: a company-name, username and password. In order to be sure that no company-name is a duplicate I want a situation where "company-A" is inserted, while at the same time prevent a situation were "CompanyA" or "Company á" can be added later. Hence I want to compress them as it were to "companya" as unique name. – Patrick Apr 18 '16 at 12:07

0 Answers0