<?php
if(isset($_POST['submit'])){
    $path = './Browse/';
    $files = glob('./Browse/*'); // get all file names
    foreach ($files as $file) { // iterate files
        if (is_file($file)) unlink($file); // delete file
        
    }
    $file = $path . basename($_FILES['spreadsheet']['name']);
    $myfile = str_replace(' ', '_', basename($_FILES['spreadsheet']['name'], ".xlsx"));
    move_uploaded_file($_FILES['spreadsheet']['tmp_name'], $file);
    require_once "./Classes/PHPExcel/IOFactory.php";
    $filepath = $path . $_FILES['spreadsheet']['name'];
    $objPHPExcel = PHPExcel_IOFactory::load($filepath);
    $sheet = $objPHPExcel->getSheet(0);
    $highestRow = $sheet->getHighestRow();
    $result = array();
    $result1 = array();
    //  Loop through each row of the worksheet in turn
    $bulk = array();
    for ($row = 1;$row <= $highestRow;$row++) {
        $str = $objPHPExcel->getActiveSheet()->getCell('A' . $row)->getValue();
        if (strpos($str, '/') != false or strpos($str, ',') !== false or strpos($str, ';') != false or strpos($str, '+') != false or strpos($str, ' ') != false or strpos($str, "or") != false or strpos($str, ":") != false) {
            $words = preg_split('/[\/,;"or":]/', $str);
            foreach ($words as $word) {
                array_push($bulk, $word);
            }
        } else {
            array_push($bulk, $str);
        }
    }
    
    
    foreach ($bulk as $str){
        $str = preg_replace("/[^0-9]/", "", $str );
        $str = ltrim($str, '0');
        
        if(strlen($str) == 9 && ((substr($str,0,2)=="50") ||(substr($str,0,2)=="52") ||(substr($str,0,2)=="54")|| (substr($str,0,2)=="55")||(substr($str,0,2)=="58")|| (substr($str,0,2)=="56"))){
            $str="971".$str;
            array_push($result,$str);continue;
        }
        if(strlen($str)==12 &&  (substr($str,0,3)=="971")&&((substr($str,3,2)=="50")||(substr($str,3,2)=="52")||(substr($str,3,2)=="54")||(substr($str,3,2)=="55")||(substr($str,3,2)=="58")||(substr($str,3,2)=="56"))){
            array_push($result,$str);continue;
        }
        if(strlen($str)==12 &&  (substr($str,0,3)=="917")&&((substr($str,3,2)=="50")||(substr($str,3,2)=="52")||(substr($str,3,2)=="54")||(substr($str,3,2)=="55")||(substr($str,3,2)=="58")||(substr($str,3,2)=="56"))){
            $str="971".substr($str,3,9);
            array_push($result,$str);continue;
        }
        if(strlen($str)==13 && substr($str,3,1)=="0" && substr($str,0,3)=="971" && (substr($str,4,2)=="50"||substr($str,4,2)=="52"||(substr($str,4,2)=="54")||substr($str,4,2)=="55"||substr($str,4,2)=="58"||substr($str,4,2)=="56")){
            $str = substr_replace($str,'',3,1);
            array_push($result,$str);continue;
        }
    }
    //sort($result);
    $input=$result;
    $result = array_unique($input);
}
?>