Passero Posted May 22, 2006 Share Posted May 22, 2006 I am writing a webapplication where the user can create templates. those templates need to be interpreted so they can create HTML. The syntax for those templates is something I chose. The question is now: how can i write a function that parse the templatecode to html? I want to extract the special template syntax and transform it to html. Do i have to do a find for each keyword that the user can use in the template and than interprete it? for example the code a lot forums use: [ B ] [ /B ] sets the text to bold so it will be transformed to <b></b>My templating code is more advanced but i think you'll know what i mean. Quote Link to comment https://forums.phpfreaks.com/topic/10181-writing-a-code-parser/ Share on other sites More sharing options...
obsidian Posted May 22, 2006 Share Posted May 22, 2006 [!--quoteo(post=376025:date=May 22 2006, 09:42 AM:name=Passero)--][div class=\'quotetop\']QUOTE(Passero @ May 22 2006, 09:42 AM) [snapback]376025[/snapback][/div][div class=\'quotemain\'][!--quotec--]I am writing a webapplication where the user can create templates. those templates need to be interpreted so they can create HTML. The syntax for those templates is something I chose. The question is now: how can i write a function that parse the templatecode to html? I want to extract the special template syntax and transform it to html. Do i have to do a find for each keyword that the user can use in the template and than interprete it? for example the code a lot forums use: [ B ] [ /B ] sets the text to bold so it will be transformed to <b></b>My templating code is more advanced but i think you'll know what i mean.[/quote]passero, welcome to the forums! if you'll look for previous discussions on how to write your own BBCode parser, you'll get the basic understanding of what to use for your template plan of attack. i'll give you a simple function that runs a BBCode parser on a string, though, so you can see where i'm coming from:[code]// This function will parse out all [b], [i], and [u] tags in BBCode formatfunction BBEncode($String){ $tags = array("|\[b\](.+?\[/b\]|i", "|\[i\](.+?\[/i\]|i", "|\[u\](.+?\[/u\]|i"); $replace = array("<strong>$1</strong>", "<em>$1</em>", "<u>$1</u>"); $String = preg_replace($tags, $replace, $String); return $String;}$par = "[b]Yo![/b] [i]Whassup???[/i] How [u]you[/u] doing?";echo BBEncode($par);[/code]hope this helps point you in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/10181-writing-a-code-parser/#findComment-37941 Share on other sites More sharing options...
Passero Posted May 23, 2006 Author Share Posted May 23, 2006 [!--quoteo(post=376026:date=May 22 2006, 03:49 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ May 22 2006, 03:49 PM) [snapback]376026[/snapback][/div][div class=\'quotemain\'][!--quotec--]passero, welcome to the forums! if you'll look for previous discussions on how to write your own BBCode parser, you'll get the basic understanding of what to use for your template plan of attack. i'll give you a simple function that runs a BBCode parser on a string, though, so you can see where i'm coming from:...[/quote]Thx for the reply but that's not what i'm looking for.. In fact the code you posted is just a "find and replace"... My template code will be more than just find and replace. For example: [code]<news number="5"> <title><h1>#TITLE#</h1></title> <item> <p>#BODY#</p> <item></news>[/code]This would mean that there wil be something like that in the output:[code]<h1>first title</h1><p>first body</p><h1>second title</h1><p>second body</p><h1>3th title</h1><p>3th body</p>[/code]You see what i mean... Quote Link to comment https://forums.phpfreaks.com/topic/10181-writing-a-code-parser/#findComment-38250 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.