예제 #1
0
function trimhousenumbers($housenumber)
{
    echo "Input: " . $housenumber . PHP_EOL;
    $numbers = preg_split('/;/', $housenumber, -1, PREG_SPLIT_NO_EMPTY);
    asort($numbers);
    natsort($numbers);
    $numbers = array_values($numbers);
    print_r($numbers);
    $hexes = array();
    $output = array();
    $alfa = array();
    $nume = array();
    $first = $last = null;
    foreach ($numbers as $this_number) {
        // filter out numbers with dot in
        $pos = strpos($this_number, '.');
        if ($pos !== false) {
            continue;
        }
        // filter out numbers with / in
        $pos = strpos($this_number, '/');
        if ($pos !== false) {
            continue;
        }
        if (ctype_digit($this_number)) {
            $nume[] = $this_number;
        } elseif (ctype_alnum($this_number)) {
            $alfa[] = $this_number;
        } else {
            // drop whatever else
        }
    }
    // echo "NUME\n";
    // print_r($nume);
    foreach ($nume as $this_number) {
        if ($first === null) {
            $first = $last = $this_number;
        }
        if ($last < $this_number - 1) {
            if (is_numeric($this_number)) {
                $output[] = $first == $last ? $first : $first . '-' . $last;
            }
            $first = $last = $this_number;
        } else {
            $last = $this_number;
        }
    }
    //if (count($output)) {
    $output[] = $first == $last ? $first : $first . '-' . $last;
    //}
    //echo "OUTPUT" . PHP_EOL;
    //print_r($output);
    foreach ($alfa as $this_number) {
        if (!is_integer($this_number)) {
            $this_number = trim($this_number);
            if (preg_match("/([0-9]+)([A-Z+])/ui", $this_number, $match)) {
                // if (preg_match("/[^[:alnum:]]/u", $this_number))
                // print_r($match);
                if (isset($match[1])) {
                    if (!isset($hexes[$match[1]], $hexes)) {
                        $hexes[$match[1]] = $match[2];
                    } else {
                        $hexes[$match[1]] .= ';' . $match[2];
                    }
                }
            }
        }
    }
    //echo "HEXES" . PHP_EOL;
    //print_r($hexes);
    //exit;
    $res = array();
    foreach ($hexes as $k => $v) {
        $ranges = array();
        $chars = preg_split('/;/', $v, -1, PREG_SPLIT_NO_EMPTY);
        $first = $last = null;
        foreach ($chars as $char) {
            // echo "ORD : (".$char .  ") -> ". ordutf8(strtoupper($char)) . " / " . utf8chr(ordutf8(strtoupper($char)))  . PHP_EOL;
            $this_char = ordutf8(strtoupper($char));
            if ($first === null) {
                $first = $last = $this_char;
            }
            if ($last < $this_char - 1) {
                $ranges[] = $first == $last ? $first : $first . '-' . $last;
                $first = $last = $this_char;
            } else {
                $last = $this_char;
            }
        }
        $ranges[] = $first == $last ? $first : $first . '-' . $last;
        $res[$k] = $ranges;
    }
    $numstr = "";
    $nnum = array();
    foreach ($res as $number => $ranges) {
        foreach ($ranges as $k => $range) {
            $newnumbers = array();
            $srange = preg_split('/-/', $range, -1, PREG_SPLIT_NO_EMPTY);
            $str_range = array();
            foreach ($srange as $kk => $vv) {
                $str_range[] = utf8chr($vv);
            }
            foreach ($str_range as $val) {
                $newnumbers[] = sprintf('%s%s', $number, $val);
            }
            $num = join('-', $newnumbers);
            $nnum[] = $num;
        }
    }
    if (is_array($nnum) && is_array($output)) {
        $nn = array_merge($output, $nnum);
        $numstr = join(';', $nn);
        return $numstr;
    } elseif (is_array($output)) {
        return $output;
    } else {
        return array();
    }
}
예제 #2
0
/**
 * Convert the string $str encoded in Windows-1256 into UTF-8
 *
 * @param string $str Windows-1256 string to convert
 * @return string the UTF-8 equivalent
 */
function w1256ToUTF8($str)
{
    static $conv = array(0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x20ac, 0x67e, 0x201a, 0x192, 0x201e, 0x2026, 0x2020, 0x2021, 0x2c6, 0x2030, 0x679, 0x2039, 0x152, 0x686, 0x698, 0x688, 0x6af, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014, 0x6a9, 0x2122, 0x691, 0x203a, 0x153, 0x200c, 0x200d, 0x6ba, 0xa0, 0x60c, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0x6be, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0x61b, 0xbb, 0xbc, 0xbd, 0xbe, 0x61f, 0x6c1, 0x621, 0x622, 0x623, 0x624, 0x625, 0x626, 0x627, 0x628, 0x629, 0x62a, 0x62b, 0x62c, 0x62d, 0x62e, 0x62f, 0x630, 0x631, 0x632, 0x633, 0x634, 0x635, 0x636, 0xd7, 0x637, 0x638, 0x639, 0x63a, 0x640, 0x641, 0x642, 0x643, 0xe0, 0x644, 0xe2, 0x645, 0x646, 0x647, 0x648, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0x649, 0x64a, 0xee, 0xef, 0x64b, 0x64c, 0x64d, 0x64e, 0xf4, 0x64f, 0x650, 0xf7, 0x651, 0xf9, 0x652, 0xfb, 0xfc, 0x200e, 0x200f, 0x6d2);
    $len = strlen($str);
    $out = "";
    for ($i = 0; $i < $len; $i++) {
        $out .= utf8chr($conv[ord($str[$i])]);
    }
    return $out;
}