1<?php
2
3/**
4 * Helper Plugin: Parse persian numbers to integer
5 *
6 * @license GPL 3 (http://www.gnu.org/licenses/gpl.html)
7 * @author  Masoud Sadrnezhaad <masoud@sadrnezhaad.ir>
8 */
9class helper_plugin_judge_numbers extends DokuWiki_Plugin
10{
11
12    public function parseNumber($number)
13    {
14        $digits = Array();
15        for ($i = 0; $i < mb_strlen($number, "UTF-8"); $i++) {
16            $digits[$i] = $this->ordutf8(mb_substr($number, $i, 1, "UTF-8"));
17            if (1776 <= $digits[$i] && $digits[$i] < 1786) {
18                $digits[$i] = $digits[$i] - 1776 + 48;
19            }
20            $digits[$i] = chr($digits[$i]);
21        }
22        return implode($digits);
23    }
24
25    public function ordutf8($string)
26    {
27        $offset = 0;
28        $code = ord(substr($string, 0, 1));
29        if ($code >= 128) {
30            if ($code < 224) {
31                $bytesnumber = 2;
32            } else if ($code < 240) {
33                $bytesnumber = 3;
34            } else if ($code < 248) {
35                $bytesnumber = 4;
36            }
37            $codetemp = $code - 192 - ($bytesnumber > 2 ? 32 : 0) - ($bytesnumber > 3 ? 16 : 0);
38            for ($i = 2; $i <= $bytesnumber; $i++) {
39                $offset++;
40                $code2 = ord(substr($string, $offset, 1)) - 128;
41                $codetemp = $codetemp * 64 + $code2;
42            }
43            $code = $codetemp;
44        }
45        $offset += 1;
46        if ($offset >= strlen($string)) {
47            $offset = -1;
48        }
49        return $code;
50    }
51}