preg_replace de gehele file
Ik ben een template systeempje aan het maken. Nu moet ik dus in php documenten nogal eens een lactatie invoeren. Dus ik dacht ik maak een preg_replace functie die bijvoorbeel {T_THEME_PATH} omzet in styles/jouw_stijl/theme.
Ik heb nu een probleem en dat is dat ik niet weet hoe ik preg_replace een gehel pagnina kan laten lezen en ook replacen.
Mijn preg_replace code in mijn function sile ziet er zo iut:
public function replaces($phpDoc)
{
$commonReplaces = array(
"T_THEME_PATH" => "styles/control/theme",
"ROOT_PATH" => $_SERVER['DOCUMENT_ROOT']."/Template/"
);
foreach($commonReplaces as $key => $value)
{
$this->phpDoc = preg_replace('#\{'.$key.'\}#Us',$value,$phpDoc);
}
}
Groetjes,
Daniël
Gewoon file_get_contents() erdoorheen halen.
Al geprobeerd. Als ik dat doe dan werkt het of helamaal niet niet of hij geeft de tekst van de pagina en dan nog eens de door de preg_replace beahndelde file_getcontents array weer
Dan zit er een fout in je andere code.
Hier ff de scripts
functions.php:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
ini_set( 'display_errors', 1 );
error_reporting( E_ALL | E_STRICT );
class template
{
public $file = '';
public $error = '';
public $getFile = false;
public $html = '';
public $phpDoc = '';
public $parseCSS = '';
public function __construct($file = false)
{
if($file != "" && $file != false)
{
if(!preg_match("/(.+?).html$/si",$file))
{
$this->error = "<b>Parse error:</b> The file has to be a html document!";
}
elseif(!file_exists($file))
{
$this->error = "<b>Parse error:</b> The file ".$file." does not exsist!";
}
$this->file = $file;
}
}
public function getFile()
{
if(file_exists($this->file))
{
$this->html = file_get_contents($this->file);
$this->getFile = true;
}
}
public function replaces($lang,$phpDoc)
{
if($this->getFile == false)
{
$this->getFile();
}
$this->html = preg_replace_callback('#\<\!-- INCLUDE (.+?).html --\>#',
create_function('$matches','return file_get_contents("styles/control/template/".$matches[1].".html");'), $this->html);
foreach($lang as $key => $value)
{
$this->html = preg_replace("#\{L_".$key."\}#s",$value,$this->html);
}
$this->phpDoc = file_get_contents($phpDoc);
$commonReplaces = array(
"ROOT_PATH" => $_SERVER['DOCUMENT_ROOT']."/Template/",
"T_THEME_PATH" => "styles/control/theme"
);
foreach($commonReplaces as $key => $value)
{
$this->phpDoc = preg_replace('#\{'.$key.'\}#Us',$value,$this->phpDoc);
}
}
public function parse()
{
if($this->error == '')
{
if($this->getFile == false)
{
$this->getFile();
}
return $this->html;
}
else
{
return $this->error;
}
}
}
?>
ini_set( 'display_errors', 1 );
error_reporting( E_ALL | E_STRICT );
class template
{
public $file = '';
public $error = '';
public $getFile = false;
public $html = '';
public $phpDoc = '';
public $parseCSS = '';
public function __construct($file = false)
{
if($file != "" && $file != false)
{
if(!preg_match("/(.+?).html$/si",$file))
{
$this->error = "<b>Parse error:</b> The file has to be a html document!";
}
elseif(!file_exists($file))
{
$this->error = "<b>Parse error:</b> The file ".$file." does not exsist!";
}
$this->file = $file;
}
}
public function getFile()
{
if(file_exists($this->file))
{
$this->html = file_get_contents($this->file);
$this->getFile = true;
}
}
public function replaces($lang,$phpDoc)
{
if($this->getFile == false)
{
$this->getFile();
}
$this->html = preg_replace_callback('#\<\!-- INCLUDE (.+?).html --\>#',
create_function('$matches','return file_get_contents("styles/control/template/".$matches[1].".html");'), $this->html);
foreach($lang as $key => $value)
{
$this->html = preg_replace("#\{L_".$key."\}#s",$value,$this->html);
}
$this->phpDoc = file_get_contents($phpDoc);
$commonReplaces = array(
"ROOT_PATH" => $_SERVER['DOCUMENT_ROOT']."/Template/",
"T_THEME_PATH" => "styles/control/theme"
);
foreach($commonReplaces as $key => $value)
{
$this->phpDoc = preg_replace('#\{'.$key.'\}#Us',$value,$this->phpDoc);
}
}
public function parse()
{
if($this->error == '')
{
if($this->getFile == false)
{
$this->getFile();
}
return $this->html;
}
else
{
return $this->error;
}
}
}
?>
index.php:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
<?php
include_once("includes/functions.php");
include("language/nl/common.php");
$nt = new template("styles/control/template/index.html");
$nt->replaces($lang, "index.php");
$nt->parseCSS("styles/control/theme/stylesheet.css");
echo $nt->parse();
include("{T_THEME_PATH}/testfile.php");
?>
include_once("includes/functions.php");
include("language/nl/common.php");
$nt = new template("styles/control/template/index.html");
$nt->replaces($lang, "index.php");
$nt->parseCSS("styles/control/theme/stylesheet.css");
echo $nt->parse();
include("{T_THEME_PATH}/testfile.php");
?>