Search code examples
phpregextemplate-engine

Regex with Dirname


Sorry for my bad english, but I want to ask a question.

I write an Templete engine for me. Now i want make that i can write @include(public/server/logik01/logik_web_init.php) and the Class Includes that file and delete the @include(....) tag.

Here my code

private static function get_include(){
    preg_match_all('/@include.*/', self::$viewMainContent, $include);
    foreach($include[0] as $include){
        $key = explode("(", $include);
        $key = explode(")", $key[1]);
        $include = addcslashes($include, "/");
        $include =  '/'.$include.'/';
        self::$viewMainContent = preg_replace($include, '', self::$viewMainContent);
        require_once($key[0]);
    }
}

in the self::$viewMainContent are this self::$viewMainContent = file_get_contents($viewMain); sourcecode.

The INclude are good, but the @include(public/server/logik01/logik_web_init.php) i can't override with preg_replace


Solution

  • instead of

     preg_match_all('/@include.*/', self::$viewMainContent, $include);
    

    Try this

    preg_match_all('/@include\((?P<include>[^)]+)\)/', '@include(public/server/logik01/logik_web_init.php)', $include);
    

    result

    include [9-49]  => `public/server/logik01/logik_web_init.php`
    

    Then you can skip all the mess and just do something like this

    To be clear you can just do something like this

     $include = array();
     preg_match_all('/@include\((?P<include>[^)]+)\)/', self::$viewMainContent, $include);
        $matches = isset($include['include']) ? count($include['include']) : 0;
        for($i = 0; $i < $matches; $i++) {
            require_once($include['include'][$i]);
        }
    

    in any case the include wont work because you dont want to replace the content with the match of the regx but with the content of the included file.

    for that you need to do this

    ob_start();
    
     require_once($include['include'][$i]);
    
    $content = ob_get_clean();
    

    And then use the full match $inclue[0] and replace it with the included content, in the original template..

    Something like this

     $include = array();
     preg_match_all('/@include\((?P<include>[^)]+)\)/', self::$viewMainContent, $include);
    $matches = count($include);
    for($i = 0; $i < $matches; $i++) {
        ob_start();
        require_once($include['include'][$i]);
        $content = ob_get_clean();
        self::$viewMainContent = preg_replace('/'.preg_quote($include[0][$i]).'/', $content, self::$viewMainContent);
    }
    

    Of course I would use file_get_contents instead of include and then do all this before processing the template for any other tags, that would exclude using php inside of included template files and you wouldn't need output buffering ob_* functions..

    Like so..

     $include = array();
     preg_match_all('/@include\((?P<include>[^)]+)\)/', self::$viewMainContent, $include);
    $matches = count($include);
    for($i = 0; $i < $matches; $i++) {
        $content = file_get_contents($include['include'][$i]);
        self::$viewMainContent = preg_replace('/'.preg_quote($include[0][$i]).'/', $content, self::$viewMainContent);
    }
    

    I haven't test this so sorry if it's not 100% correct but should get you down the right road.