-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTranslit.php
More file actions
75 lines (63 loc) · 2.06 KB
/
Translit.php
File metadata and controls
75 lines (63 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace dastanaron\translit;
/**
* This is just an example.
*/
class Translit
{
public $rus;
public $lat;
private $route;
public function __construct() {
$this->rus = [
'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И',
'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т',
'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь',
'Э', 'Ю', 'Я', 'а', 'б', 'в', 'г', 'д', 'е', 'ё',
'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п',
'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ',
'ъ', 'ы', 'ь', 'э', 'ю', 'я'
];
$this->lat = [
'A', 'B', 'V', 'G', 'D', 'E', 'E', 'Gh', 'Z', 'I',
'Y', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T',
'U', 'F', 'H', 'C', 'Ch', 'Sh', 'Sch', 'Y', 'Y',
'Y', 'E', 'Yu', 'Ya', 'a', 'b', 'v', 'g', 'd', 'e',
'e', 'gh', 'z', 'i', 'y', 'k', 'l', 'm', 'n', 'o',
'p', 'r', 's', 't', 'u', 'f', 'h', 'c', 'ch', 'sh',
'sch', 'y', 'y', 'y', 'e', 'yu', 'ya'
];
}
public function translit($str, $nonspace=true, $route='')
{
$string = '';
if(empty($route)) {
$this->route($str);
}
else {
$this->route = $route;
}
if($this->route == 'en-ru') {
$string = str_replace($this->lat, $this->rus, $str);
}
elseif($this->route == 'ru-en') {
$string = str_replace($this->rus, $this->lat, $str);
}
else {
throw new Exception('Неизвестное направление транслитерации');
}
if ($nonspace === true) {
$string = str_replace(' ', '_', $string);
}
return $string;
}
private function route($str)
{
if(preg_match('#[A-Za-z]+#', $str)) {
$this->route = 'en-ru';
}
else {
$this->route = 'ru-en';
}
}
}