-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert-numbers.php
114 lines (73 loc) · 3.1 KB
/
convert-numbers.php
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
// Convert Farsi ( Persian ), English and Arabic Numbers
if ( !class_exists( 'convert_numbers' ) ) {
class convert_numbers {
private $arabic_numbers = array( '٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩', '٫' );
private $farsi_numbers = array( '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹', '٫' );
private $english_numbers = array( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.');
private $string = '';
public function __construct( $string = '' ) {
$this->string = $string;
}
/* Set New String */
public function set( $string = '' ) {
$this->string = $string;
}
/* Get Farsi ( Persian ) Numbers */
public function to_farsi() {
$farsi = $this->arabic_to_farsi( $this->english_to_farsi( $this->string ) );
return $farsi;
}
/* Get Arabic Numbers */
public function to_arabic() {
$farsi = $this->farsi_to_arabic( $this->english_to_arabic( $this->string ) );
return $farsi;
}
/* Get English Numbers */
public function to_english() {
$farsi = $this->arabic_to_english( $this->farsi_to_english( $this->string ) );
return $farsi;
}
/* Convert Arabic to Farsi ( Persian ) */
private function arabic_to_farsi( $string = '' ) {
if ( empty( $string ) ) $string = $this->string;
return str_replace( $this->arabic_numbers, $this->farsi_numbers, $string );
}
/* Convert Farsi ( Persian ) to Arabic */
private function farsi_to_arabic( $string = '' ) {
if ( empty( $string ) ) $string = $this->string;
return str_replace( $this->farsi_numbers, $this->arabic_numbers, $string );
}
/* Convert English to Farsi ( Persian ) */
private function english_to_farsi( $string = '' ) {
if ( empty( $string ) ) $string = $this->string;
return str_replace( $this->english_numbers, $this->farsi_numbers, $string );
}
/* Convert Farsi ( Persian ) to English */
private function farsi_to_english( $string = '' ) {
if ( empty( $string ) ) $string = $this->string;
return str_replace( $this->farsi_numbers, $this->english_numbers, $string );
}
/* Convert English to Arabic */
private function english_to_arabic( $string = '' ) {
if ( empty( $string ) ) $string = $this->string;
return str_replace( $this->english_numbers, $this->arabic_numbers, $string );
}
/* Convert Arabic to English */
private function arabic_to_english( $string = '' ) {
if ( empty( $string ) ) $string = $this->string;
return str_replace( $this->arabic_numbers, $this->english_numbers, $string );
}
}
}
// Samples
$convert_numbers = new convert_numbers( '2018-07-18' );
echo $convert_numbers->to_farsi(); // ۲۰۱۸-۰۷-۱۸
echo $convert_numbers->to_arabic(); // ٢٠١٨-٠٧-١٨
$convert_numbers->set( '١٤٣٩/١١/٠٤' ); // Set Arabic String
echo $convert_numbers->to_farsi(); // ۱۴۳۹/۱۱/۰۴
echo $convert_numbers->to_english(); // 1439/11/04
$convert_numbers->set( '۱۳۹۷/۰۴/۲۷' ); // Set Farsi String
echo $convert_numbers->to_arabic(); // ١٣٩٧/٠٤/٢٧
echo $convert_numbers->to_english(); // 1397/04/27
?>