koorb
11-16-2003, 10:13 AM
What pattern would i use to match a string like
{if condition}
<html>
{else}
<other html>
{endif}
where the
{else}
<other html>
is optional, can I do this with just one pattern
John[H4Y]
11-16-2003, 12:35 PM
I assume you are talking about doing one thing if you match <html>, another if you match <any other tag>. I might have totally misinterpretted your post.. if so, let me know.
in perl:
$html = "some html";
if ($html =~ m/<html>/i) {
&dosomething;
}
elsif ($html =~ m/<[^<>]*>/) {
&dosomethingelse;
}
in php:
<?php
$html = "some html";
if (eregi("<html>", $html)) {
dosomething();
}
elseif (ereg("<[^<>]*>", $html)) {
dosomethingelse();
}
?>
If there is a tag in $html with anything but <html>, it will perform the dosomethingelse function, but with no match it would just continue executing without executing dosomething() or dosomethingelse().
If I didn't understand what you were asking and you were referring to actually matching the phrase:
"{if condition}
<html>
{else}
<other html>
{endif}", the only problem is I don't know what type of input you are expecting exactly (are you trying to match "if condition" literally or would the "codition" part change)? Please provide more details.
koorb
11-16-2003, 04:23 PM
i will try to explain better,
I am creating a faily simple template system and want to be able to use some basic control structures in the templates.
Literaly in the template file
<html>
.. html stuff
{if condition}
.. do this stuff ...
{else}
.. do this stuff ...
{endif}
..more html things...
</html>
// eof
so I need to (in php) match the {if} statements extracting the `condition` to evaluate and each `.. do this stuff ...` to do if the condition is true or false. I have got it basicly but i cannot have any other {} template code in bettween. the pattern i am using at the moment is:
/\{if([^\r\n\}\{]+)\}([^\{\}]+)(?:\{else\}([^\{\}]+))?\{\/if\}/
if you can read through that :D basicly matching
{if any character that isnt a new line or return}
any character that isnt { or }
//optionaly//
{/else}
any character that isnt { or }
//optionaly//
{/if}
any help in getting this to work better would be good, i want to be able to use other { } structures within each other.
Same situation with
{loop array}
..loop this stuff..
{/loop}
works but cant have any other { } `tags` in bettween
Burhan
11-16-2003, 05:18 PM
Smarty (http://smarty.php.net) does what you want (with a smiliar tag syntax). Trying to reinvent the wheel? :D
koorb
11-16-2003, 07:04 PM
to put it simply smarty is too big :D
Im not doing anything major, im just realy anal about doing things myself (he says having just used the phpmailer class :D) no but i have looked for an appropriate template class, what i have will do, i wasnt after to advanced template coding so i think i will be ok, I got kinda caried away :D
hiryuu
11-16-2003, 11:45 PM
If GPL code is okay with you, I would suggest dissecting smarty and pulling what you need. I have this sinking feeling it looks more like a compiler than a regex. That probably explains a lot about its size.