Memurame
Memurame

Reputation: 53

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

Upvotes: 0

Views: 80

Answers (2)

Memurame
Memurame

Reputation: 53

Thx

The Template code are like this one.

<html>
<head>
    <title>Test</title>
    <link rel="stylesheet" type="text/css" href="{WEBROOT}/css/bundle.css">
</head>
<body>
  @include({SRVROOT}/public/server/logik01/logik_web_init.php)
  <div id="wrapper">
    <div id="header">
      <div><img src="{WEBROOT}/img/mechtronik_2012.png"></div>
      <div>E-Shop Stromschienen</div>
      <div><a href="#" id="btnLogout"><img src="{WEBROOT}/img/grey_log-out1.png"><span>Logout</span></a></div>
    </div>
    <div id="content">
      <div class="flex">
        <div class="border-panel">
          <legend>Login</legend>
          <button id="startLogik" class="button" name="start">Konfigurator starten</button>
        </div>
      </div>
    </div>
  </div>
  <script type="text/javascript" src="{WEBROOT}/js/logikscripte.js"></script>
</body>

And i want i can more tha one @include

It works now with this code

$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]);
        self::$viewMainContent = preg_replace('/'.preg_quote($include[0][$i], '/').'/', '', self::$viewMainContent);
}

Upvotes: 0

ArtisticPhoenix
ArtisticPhoenix

Reputation: 21681

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.

Upvotes: 1

Related Questions