Search code examples
phpregexgetpreg-replace-callback

preg_replace_callback number 404 as value


Thing for me is not clear. Before I just used preg_replace but on a newer version of php had to use preg_replace_callback after that happened to me this

PHP message: PHP Warning: preg_replace_callback(): Requires argument 2, '[$1_1]', to be a valid callback in

this &c=jQuery31102606949325169924_1480249921485?_=1484049921486

$this->aURI = explode('/', $_SERVER['SCRIPT_NAME']);
$sRequest = preg_replace('!'.$this->sURI.'(.*)$!i',  '$1', $_SERVER['REQUEST_URI']);
        if(substr($sRequest, -1)!='/')
            $sRequest .= '/';
        $sGets = $this->parseUrl($sRequest);

I dont know but i figure out when i insert url which has number 404 as value in any place I have a white page

    private function parseUrl($sRequest){   
    $sVars = null;
    foreach($this->aRoutingParse AS $k => $v){

        if(!is_array($v))
            continue;

        preg_match_all('!\[(.+?)\]!i', $v[0], $aExpression_);
        $sExpression = preg_replace_callback('!\[(.+?)\]!i', function($m) use ($k){ 
            return $this->transformParam($m[1], $k);
        }, $v[0]);


        if(preg_match_all('!'.$sExpression.'!i', $sRequest, $aExpression__)){


            foreach($aExpression__ AS $k_ => $v_){
                foreach($v_ AS $kkk => $vvv){

                    if(!isset($aExpression_[1][$k_-1]))
                        $aExpression_[1][$k_-1] = null;

                    if($kkk>0)
                        $aExpression[] = array($aExpression_[1][$k_-1].'_'.$kkk, $vvv);
                    else
                        $aExpression[] = array($aExpression_[1][$k_-1], $vvv);

                }
            }

            unset($aExpression[0]);
            $iCount = count($aExpression__[0]);
            if($iCount>1){
                for($i=0;$i<$iCount;$i++){
                    if($i>0)
                        $sVars .= '&'.preg_replace_callback('!\[(.+?)\]!i', '[$1_'.$i.']', $v[1]);
                    else
                        $sVars = '&'.$v[1];                        
                }

            }else                
                $sVars = '&'.$v[1];

            foreach($aExpression AS $k => $v_){
                if(!isset($v['_'.$v_[0]]))
                    $v['_'.$v_[0]] = null;

                if(!is_array($v['_'.$v_[0]]))
                    $sVars = str_replace('['.$v_[0].']', $v_[1], $sVars);

                else {
                    $this->aRoutingParse = array($v['_'.$v_[0]]);
                    $sVars = $sVars.$this->parseUrl($v_[1]);

                }
            }                
            break;
        }
    }    

    return $sVars;
}

solution:

method return first the first found value in $this->aRoutingParse i have this and somehow crash array('404' => array('404', 'task=page&action=404'),


Solution

  • The preg_replace_callback expects a callback function as the second argument. You pass a string replacement pattern in preg_replace_callback('!\[(.+?)\]!i', '[$1_'.$i.']', $v[1]).

    Instead preg_replace_callback('!\[(.+?)\]!i', '[$1_'.$i.']', $v[1]) use:

    preg_replace('!\[(.+?)\]!i', '[$1_'.$i.']', $v[1])