$isdn_applications$isdn_applications are objects that hold a name and number for an application. (same for sip_sip_)
$isdn_regexp$isdn_regexp hold the regexp that are built from the numbers. (note: numbers itself are stored and hold regexp, only / / is missing for preg_match which is there attached).
$isdn_applications are objects that hold a name and number for an application. (same for sip_) $isdn_regexp hold the regexp that are built from the numbers. (note: numbers itself are stored and hold regexp, only / / is missing for preg_match which is there attached).
$isdn_applications are objects that hold a name and number for an application. (same for sip_)
$isdn_regexp hold the regexp that are built from the numbers. (note: numbers itself are stored and hold regexp, only / / is missing for preg_match which is there attached).
$start_time = microtime(true);  // Startzeit
                
                
                $rangeStart = $_POST["search_numberrange_start"];
                $rangeEnd = $_POST["search_numberrange_end"];
                
                $this->applicationManager->sortAppsByNumber();
                $isdn_applications = $this->applicationManager->getISDNapps();
                $sip_applications = $this->applicationManager->getSIPapps();                
                
                $isdn_regexp = array();
                $sip_regexp = array();
                
                for($i = 0; $i < count($isdn_applications); $i++)
                    array_push($isdn_regexp, "/".$isdn_applications[$i]->getNumber()."/");
                
                for($i = 0; $i < count($sip_applications); $i++)
                    array_push($sip_regexp, "/".$sip_applications[$i]->getNumber()."/");
                
                $matched_sip = array();
                $matched_isdn = array();
                    
                $number = $rangeStart;
                
                do {
                    
                    $matched = false;
                    $code = $number;
                    
                    if($this->numberRangeCheckType == "default" || $this->numberRangeCheckType == "isdn") {
                        for($i = 0; $i < count($isdn_regexp); $i++) {
                            $regExp = $isdn_regexp[$i];
                            
                            if(preg_match($regExp$isdn_regexp[$i],$number)) {
                                $code = $code." <a href='#application_".$i."' class='applink'>".$isdn_applications[$i]->getName()."</a>";
                                $matched = true;
                            }
                        }
                    }
                    
                    if(!$matched) {
                        $code = "<li class='free'>".$number." FREI</li>";
                        array_push($matched_isdn, array("code" => $code));
                    }
                    else {
                        $code = "<li class='occupied'>".$code."</li>";
                        array_push($matched_isdn, array("code" => $code));
                    }
                    
                    $code = $number;
                    $matched = false;
                    
                    if($this->numberRangeCheckType == "default" || $this->numberRangeCheckType == "sip") {
                        for($i = 0; $i < count($sip_regexp); $i++) {
                            $regExp = $sip_regexp[$i];
                            
                            if(preg_match($regExp$sip_regexp[$i],$number)) {
                                $code = $code." <a href='#application_".$i."' class='applink'>".$sip_applications[$i]->getName()."</a>";
                                $matched = true;
                            }
                        }
                    }
                    
                    if(!$matched) {
                        $code = "<li class='free'>".$number." FREI</li>";
                        array_push($matched_sip, array("code" => $code));
                    }
                    else {
                        $code = "<li class='occupied'>".$code."</li>";
                        array_push($matched_sip, array("code" => $code));
                    }
                    
                    $number++;
                }while($number < $rangeEnd);
                
                $end_time = microtime(true);
                $time = $end_time - $start_time;
                echo "<p>Seite generiert in ".round($time, 5)." Sekunden</p>";  
                
                /*switch($this->numberRangeCheckType) {
                    case "isdn":    echo "<p class='match_caption'>ISDN:</p>";
                                    $this->printMatchedEntry($matched_isdn);
                                    break;
                    
                    case "sip":     echo "<p class='match_caption'>SIP:</p>";
                                    $this->printMatchedEntry($matched_sip);
                                    break;
                    
                    case "default": echo "<p class='match_caption'>ISDN:</p>";
                                    $this->printMatchedEntry($matched_isdn);
                                    echo "<p class='match_caption'>SIP:</p>";
                                    $this->printMatchedEntry($matched_sip);
                                    break;
                    }   */      
                
            $end_time = microtime(true);
            $time = $end_time - $start_time;
$start_time = microtime(true);  // Startzeit
                
                
                $rangeStart = $_POST["search_numberrange_start"];
                $rangeEnd = $_POST["search_numberrange_end"];
                
                $this->applicationManager->sortAppsByNumber();
                $isdn_applications = $this->applicationManager->getISDNapps();
                $sip_applications = $this->applicationManager->getSIPapps();                
                
                $isdn_regexp = array();
                $sip_regexp = array();
                
                for($i = 0; $i < count($isdn_applications); $i++)
                    array_push($isdn_regexp, "/".$isdn_applications[$i]->getNumber()."/");
                
                for($i = 0; $i < count($sip_applications); $i++)
                    array_push($sip_regexp, "/".$sip_applications[$i]->getNumber()."/");
                
                $matched_sip = array();
                $matched_isdn = array();
                    
                $number = $rangeStart;
                
                do {
                    
                    $matched = false;
                    $code = $number;
                    
                    if($this->numberRangeCheckType == "default" || $this->numberRangeCheckType == "isdn") {
                        for($i = 0; $i < count($isdn_regexp); $i++) {
                            $regExp = $isdn_regexp[$i];
                            
                            if(preg_match($regExp,$number)) {
                                $code = $code." <a href='#application_".$i."' class='applink'>".$isdn_applications[$i]->getName()."</a>";
                                $matched = true;
                            }
                        }
                    }
                    
                    if(!$matched) {
                        $code = "<li class='free'>".$number." FREI</li>";
                        array_push($matched_isdn, array("code" => $code));
                    }
                    else {
                        $code = "<li class='occupied'>".$code."</li>";
                        array_push($matched_isdn, array("code" => $code));
                    }
                    
                    $code = $number;
                    $matched = false;
                    
                    if($this->numberRangeCheckType == "default" || $this->numberRangeCheckType == "sip") {
                        for($i = 0; $i < count($sip_regexp); $i++) {
                            $regExp = $sip_regexp[$i];
                            
                            if(preg_match($regExp,$number)) {
                                $code = $code." <a href='#application_".$i."' class='applink'>".$sip_applications[$i]->getName()."</a>";
                                $matched = true;
                            }
                        }
                    }
                    
                    if(!$matched) {
                        $code = "<li class='free'>".$number." FREI</li>";
                        array_push($matched_sip, array("code" => $code));
                    }
                    else {
                        $code = "<li class='occupied'>".$code."</li>";
                        array_push($matched_sip, array("code" => $code));
                    }
                    
                    $number++;
                }while($number < $rangeEnd);
                
                $end_time = microtime(true);
                $time = $end_time - $start_time;
                echo "<p>Seite generiert in ".round($time, 5)." Sekunden</p>";  
                
                /*switch($this->numberRangeCheckType) {
                    case "isdn":    echo "<p class='match_caption'>ISDN:</p>";
                                    $this->printMatchedEntry($matched_isdn);
                                    break;
                    
                    case "sip":     echo "<p class='match_caption'>SIP:</p>";
                                    $this->printMatchedEntry($matched_sip);
                                    break;
                    
                    case "default": echo "<p class='match_caption'>ISDN:</p>";
                                    $this->printMatchedEntry($matched_isdn);
                                    echo "<p class='match_caption'>SIP:</p>";
                                    $this->printMatchedEntry($matched_sip);
                                    break;
                    }   */      
                
            $end_time = microtime(true);
            $time = $end_time - $start_time;
$start_time = microtime(true);  // Startzeit
                
                
                $rangeStart = $_POST["search_numberrange_start"];
                $rangeEnd = $_POST["search_numberrange_end"];
                
                $this->applicationManager->sortAppsByNumber();
                $isdn_applications = $this->applicationManager->getISDNapps();
                $sip_applications = $this->applicationManager->getSIPapps();                
                
                $isdn_regexp = array();
                $sip_regexp = array();
                
                for($i = 0; $i < count($isdn_applications); $i++)
                    array_push($isdn_regexp, "/".$isdn_applications[$i]->getNumber()."/");
                
                for($i = 0; $i < count($sip_applications); $i++)
                    array_push($sip_regexp, "/".$sip_applications[$i]->getNumber()."/");
                
                $matched_sip = array();
                $matched_isdn = array();
                    
                $number = $rangeStart;
                
                do {
                    
                    $matched = false;
                    $code = $number;
                    
                    if($this->numberRangeCheckType == "default" || $this->numberRangeCheckType == "isdn") {
                        for($i = 0; $i < count($isdn_regexp); $i++) {
                            
                            if(preg_match($isdn_regexp[$i],$number)) {
                                $code = $code." <a href='#application_".$i."' class='applink'>".$isdn_applications[$i]->getName()."</a>";
                                $matched = true;
                            }
                        }
                    }
                    
                    if(!$matched) {
                        $code = "<li class='free'>".$number." FREI</li>";
                        array_push($matched_isdn, array("code" => $code));
                    }
                    else {
                        $code = "<li class='occupied'>".$code."</li>";
                        array_push($matched_isdn, array("code" => $code));
                    }
                    
                    $code = $number;
                    $matched = false;
                    
                    if($this->numberRangeCheckType == "default" || $this->numberRangeCheckType == "sip") {
                        for($i = 0; $i < count($sip_regexp); $i++) {
                            
                            if(preg_match($sip_regexp[$i],$number)) {
                                $code = $code." <a href='#application_".$i."' class='applink'>".$sip_applications[$i]->getName()."</a>";
                                $matched = true;
                            }
                        }
                    }
                    
                    if(!$matched) {
                        $code = "<li class='free'>".$number." FREI</li>";
                        array_push($matched_sip, array("code" => $code));
                    }
                    else {
                        $code = "<li class='occupied'>".$code."</li>";
                        array_push($matched_sip, array("code" => $code));
                    }
                    
                    $number++;
                }while($number < $rangeEnd);
                
                $end_time = microtime(true);
                $time = $end_time - $start_time;
                echo "<p>Seite generiert in ".round($time, 5)." Sekunden</p>";  
                
                /*switch($this->numberRangeCheckType) {
                    case "isdn":    echo "<p class='match_caption'>ISDN:</p>";
                                    $this->printMatchedEntry($matched_isdn);
                                    break;
                    
                    case "sip":     echo "<p class='match_caption'>SIP:</p>";
                                    $this->printMatchedEntry($matched_sip);
                                    break;
                    
                    case "default": echo "<p class='match_caption'>ISDN:</p>";
                                    $this->printMatchedEntry($matched_isdn);
                                    echo "<p class='match_caption'>SIP:</p>";
                                    $this->printMatchedEntry($matched_sip);
                                    break;
                    }   */      
                
            $end_time = microtime(true);
            $time = $end_time - $start_time;
Lots of RegEx match against huge number range (PHP)
I have to check a given amount of regular expressions, which are defining number ranges for dial plans, against an input number range. The target is to check and see, if any of the numbers in the range are free, or which regular expression matches for the number. Last situation is said to be an occupied number, otherwise if no regex matches, the number is defined as free.
I match every number from the input range against every regexp and generate some output code. The main problem is, that this sort of "brute force" causes huge load on server and for 50.000 numbers the default php timeout is thrown. Anyhow, this is in my eyes also too long to wait for. The output is not even the real problem (but could and will get optimized anyhow), but for now I need a more efficient way to match.
Here is my code:
$start_time = microtime(true);  // Startzeit
                
                
                $rangeStart = $_POST["search_numberrange_start"];
                $rangeEnd = $_POST["search_numberrange_end"];
                
                $this->applicationManager->sortAppsByNumber();
                $isdn_applications = $this->applicationManager->getISDNapps();
                $sip_applications = $this->applicationManager->getSIPapps();                
                
                $isdn_regexp = array();
                $sip_regexp = array();
                
                for($i = 0; $i < count($isdn_applications); $i++)
                    array_push($isdn_regexp, "/".$isdn_applications[$i]->getNumber()."/");
                
                for($i = 0; $i < count($sip_applications); $i++)
                    array_push($sip_regexp, "/".$sip_applications[$i]->getNumber()."/");
                
                $matched_sip = array();
                $matched_isdn = array();
                    
                $number = $rangeStart;
                
                do {
                    
                    $matched = false;
                    $code = $number;
                    
                    if($this->numberRangeCheckType == "default" || $this->numberRangeCheckType == "isdn") {
                        for($i = 0; $i < count($isdn_regexp); $i++) {
                            $regExp = $isdn_regexp[$i];
                            
                            if(preg_match($regExp,$number)) {
                                $code = $code." <a href='#application_".$i."' class='applink'>".$isdn_applications[$i]->getName()."</a>";
                                $matched = true;
                            }
                        }
                    }
                    
                    if(!$matched) {
                        $code = "<li class='free'>".$number." FREI</li>";
                        array_push($matched_isdn, array("code" => $code));
                    }
                    else {
                        $code = "<li class='occupied'>".$code."</li>";
                        array_push($matched_isdn, array("code" => $code));
                    }
                    
                    $code = $number;
                    $matched = false;
                    
                    if($this->numberRangeCheckType == "default" || $this->numberRangeCheckType == "sip") {
                        for($i = 0; $i < count($sip_regexp); $i++) {
                            $regExp = $sip_regexp[$i];
                            
                            if(preg_match($regExp,$number)) {
                                $code = $code." <a href='#application_".$i."' class='applink'>".$sip_applications[$i]->getName()."</a>";
                                $matched = true;
                            }
                        }
                    }
                    
                    if(!$matched) {
                        $code = "<li class='free'>".$number." FREI</li>";
                        array_push($matched_sip, array("code" => $code));
                    }
                    else {
                        $code = "<li class='occupied'>".$code."</li>";
                        array_push($matched_sip, array("code" => $code));
                    }
                    
                    $number++;
                }while($number < $rangeEnd);
                
                $end_time = microtime(true);
                $time = $end_time - $start_time;
                echo "<p>Seite generiert in ".round($time, 5)." Sekunden</p>";  
                
                /*switch($this->numberRangeCheckType) {
                    case "isdn":    echo "<p class='match_caption'>ISDN:</p>";
                                    $this->printMatchedEntry($matched_isdn);
                                    break;
                    
                    case "sip":     echo "<p class='match_caption'>SIP:</p>";
                                    $this->printMatchedEntry($matched_sip);
                                    break;
                    
                    case "default": echo "<p class='match_caption'>ISDN:</p>";
                                    $this->printMatchedEntry($matched_isdn);
                                    echo "<p class='match_caption'>SIP:</p>";
                                    $this->printMatchedEntry($matched_sip);
                                    break;
                    }   */      
                
            $end_time = microtime(true);
            $time = $end_time - $start_time;
$isdn_applications are objects that hold a name and number for an application. (same for sip_) $isdn_regexp hold the regexp that are built from the numbers. (note: numbers itself are stored and hold regexp, only / / is missing for preg_match which is there attached).
Feel free to ask questions if something is not clearly enough and thanks for taking time!