regex phpBB style IF/ELSE
Ik ben nog een beginnende met regex, ik heb deze tutorial gelezen, en met die info ben ik aan de slag gegaan. Wat ik wil bekomen is (zoals in het phpBB template systeem)
<!-- IF ( VAR == 1 ) -->
var = 1;
<!-- ELSE -->
$var != 1;
<!-- ENDIF -->
of
<!-- IF ( VAR == 1 ) -->
var = 1;
<!-- ENDIF -->
daarvoor doorzoek ik de pagina met deze regex :
preg_match('/<!\-\-[\s|]IF[\s][\(|][\s|](.+)[\s|](\=\=|\!\=)[\s|](.+)[\s|][\)|][\s]\-\->/i', $var, $test);
daarmee bekom ik :
Array
(
[0] => <!-- IF ( VAR == 1 ) -->
[1] => VAR
[2] => ==
[3] => 1
)
Die werkt goed dus, mss nog wel wat betere manieren, maar goed. Het 2de deel is dan :
$patt = '/<!\-\-[\s|]IF[\s][\(|][\s|]' . $test['1'] . '[\s|]' . $test['2'] . '[\s|]' . $test['3'] . '[\s|][\)|][\s]\-\->(.+)(<!\-\-[\s|]ELSE[\s|]\-\->(.+)<!\-\-[\s|]ENDIF[\s|]\-\->|<!\-\-[\s|]ENDIF[\s|]\-\->)/is';
preg_match($patt, $var , $a);
Die werkt echter nietgoed, en het probleem zit erin dat ik niet echt goed weet hoe ik de keuze laat, namelijk het kan zijn dat ELSE is, maar kan ook zijn dat het ENDIF is.
Iemand tips, of eventueel een "gevorderde" tutorial die mij kan helpen ?
alvast bedankt,
vriendelijke groeten,
Svenn
Kijk eens hoe phpbb het aanpakt.
Toch bedankt voor je reply.
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
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
<?php
$var = "
<!-- IF ( A == B ) -->
test
<!-- ELSE -->
anders
<!-- ENDIF -->
";
#$var = "<!-- IF a != b -->";
#$var = "<!-- IF a == b -->";
preg_match ('/<!--[\s|]IF[\s|][\(|][\s|](.*?)[\s|](==|!=)[\s|](.*?)[\s|][\)|][\s|]-->(.*?)(<!-- ELSE -->(.*?)<!--[\s|]ENDIF[\s|]-->|<!--[\s|]ENDIF[\s|]-->)/is', $var, $test_b);
$template = array ("A" => 123, "B" => 5);
switch ($test_b['2']){
case "==":
$a = isset($template[$test_b['1']]) ? $template[$test_b['1']] : (int) $test_b['1'];
$b = isset($template[$test_b['3']]) ? $template[$test_b['3']] : (int) $test_b['3'];
if ( $a == $b )
{
$output = $test_b['4'];
}
else
{
$output = ($a = preg_match ('/(<!--[\s|]ELSE[\s|]-->)/', $test_b['5'])) ? $test_b['6']: '';
}
break;
case "!=":
$a = isset($template[$test_b['1']]) ? $template[$test_b['1']] : (int) $test_b['1'];
$b = isset($template[$test_b['3']]) ? $template[$test_b['3']] : (int) $test_b['3'];
if ( $a != $b )
{
$output = $test_b['4'];
}
else
{
$output = (preg_match ('/(<!--[\s|]ELSE[\s|]-->)/', $test_b['5'])) ? $test_b['6']: '';
}
break;
}
$pattern = '/<!--[\s|]IF[\s|][\(|][\s|]' . $test_b['1'] . '[\s|]' . $test_b['2'] . '[\s|]' . $test_b['3'] . '[\s|][\)|][\s|]-->(.*?)(<!-- ELSE -->(.*?)<!--[\s|]ENDIF[\s|]-->|<!--[\s|]ENDIF[\s|]-->)/is';
echo preg_replace($pattern, $output, $var);
?>
$var = "
<!-- IF ( A == B ) -->
test
<!-- ELSE -->
anders
<!-- ENDIF -->
";
#$var = "<!-- IF a != b -->";
#$var = "<!-- IF a == b -->";
preg_match ('/<!--[\s|]IF[\s|][\(|][\s|](.*?)[\s|](==|!=)[\s|](.*?)[\s|][\)|][\s|]-->(.*?)(<!-- ELSE -->(.*?)<!--[\s|]ENDIF[\s|]-->|<!--[\s|]ENDIF[\s|]-->)/is', $var, $test_b);
$template = array ("A" => 123, "B" => 5);
switch ($test_b['2']){
case "==":
$a = isset($template[$test_b['1']]) ? $template[$test_b['1']] : (int) $test_b['1'];
$b = isset($template[$test_b['3']]) ? $template[$test_b['3']] : (int) $test_b['3'];
if ( $a == $b )
{
$output = $test_b['4'];
}
else
{
$output = ($a = preg_match ('/(<!--[\s|]ELSE[\s|]-->)/', $test_b['5'])) ? $test_b['6']: '';
}
break;
case "!=":
$a = isset($template[$test_b['1']]) ? $template[$test_b['1']] : (int) $test_b['1'];
$b = isset($template[$test_b['3']]) ? $template[$test_b['3']] : (int) $test_b['3'];
if ( $a != $b )
{
$output = $test_b['4'];
}
else
{
$output = (preg_match ('/(<!--[\s|]ELSE[\s|]-->)/', $test_b['5'])) ? $test_b['6']: '';
}
break;
}
$pattern = '/<!--[\s|]IF[\s|][\(|][\s|]' . $test_b['1'] . '[\s|]' . $test_b['2'] . '[\s|]' . $test_b['3'] . '[\s|][\)|][\s|]-->(.*?)(<!-- ELSE -->(.*?)<!--[\s|]ENDIF[\s|]-->|<!--[\s|]ENDIF[\s|]-->)/is';
echo preg_replace($pattern, $output, $var);
?>
http://wiki.phpfreakz.nl/Regexes:_Veelgemaakte_fouten#Het_matchen_op_.60..2A.60
Dankje, ik ga deze eens lezen, en m'n poging hier plaatsen :)