Jump to content


Photo

writing a code parser


  • Please log in to reply
2 replies to this topic

#1 Passero

Passero
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 22 May 2006 - 01:42 PM

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.

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 22 May 2006 - 01:49 PM

[!--quoteo(post=376025:date=May 22 2006, 09:42 AM:name=Passero)--][div class=\'quotetop\']QUOTE(Passero @ May 22 2006, 09:42 AM) View Post[/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:
// 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);

hope this helps point you in the right direction.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 Passero

Passero
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 23 May 2006 - 02:22 PM

[!--quoteo(post=376026:date=May 22 2006, 03:49 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ May 22 2006, 03:49 PM) View Post[/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:


<news number="5">  
    <title><h1>#TITLE#</h1></title>
    <item>
       <p>#BODY#</p>
    <item>
</news>

This would mean that there wil be something like that in the output:

<h1>first title</h1>
<p>first body</p>
<h1>second title</h1>
<p>second body</p>
<h1>3th title</h1>
<p>3th body</p>

You see what i mean...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users