Jump to content

writing a code parser


Passero

Recommended Posts

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.
Link to comment
Share on other sites

[!--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 format
function 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.
Link to comment
Share on other sites

[!--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...
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.