Need a parser idea
Moderator: General Moderators
-
fastfingertips
- Forum Contributor
- Posts: 242
- Joined: Sun Dec 28, 2003 1:40 am
- Contact:
Need a parser idea
I want to create a system that allows to write something like this:
<engine:text text="Welcome"/>
instead of
<?php echo "Welcome"; ?>
I'm using this notation (close to XML) just because i think that is looking ok and i do not have any intention to create XML docs from it.
How do you advice me to do this?
<engine:text text="Welcome"/>
instead of
<?php echo "Welcome"; ?>
I'm using this notation (close to XML) just because i think that is looking ok and i do not have any intention to create XML docs from it.
How do you advice me to do this?
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
-
fastfingertips
- Forum Contributor
- Posts: 242
- Joined: Sun Dec 28, 2003 1:40 am
- Contact:
Because of this
<engine:text textId="12"/>
And in this is replacing me a function:
<?php getTextId(12); ?>
I'm making the switching because for a user that is not close to my application will be easier to use first sentence instead of the second one.
Also because the code behind at what i'm thinking should look like:This extension is EXPERIMENTAL. The behaviour of this extension -- including the names of its functions and anything else documented about this extension -- may change without notice in a future release of PHP. Use this extension at your own risk.
<engine:text textId="12"/>
And in this is replacing me a function:
<?php getTextId(12); ?>
I'm making the switching because for a user that is not close to my application will be easier to use first sentence instead of the second one.
You're going to want regular expressions for this one. Use preg_replace_callback (or another of the preg functions) to locate your tags, then it's a simple matter of interpreting them, and replacing them with the correct output. Write back if you need more info - there are lots of talented people on this forum.
PHP's XML DOM is actually quite powerful and very stable for generating XML. To parse it, install the sablotron-module. You can then parse XML with XLS or XPath.fastfingertips wrote:Because of this
Also because the code behind at what i'm thinking should look like:This extension is EXPERIMENTAL. The behaviour of this extension -- including the names of its functions and anything else documented about this extension -- may change without notice in a future release of PHP. Use this extension at your own risk.
<engine:text textId="12"/>
And in this is replacing me a function:
<?php getTextId(12); ?>
I'm making the switching because for a user that is not close to my application will be easier to use first sentence instead of the second one.
no need to re-invent the wheel - unless you're hellbent on doing that anyway...
-
fastfingertips
- Forum Contributor
- Posts: 242
- Joined: Sun Dec 28, 2003 1:40 am
- Contact:
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
fast - it's quite a simple idea, really. Use regular expressions to match the custom tags you have created. Once you have a pattern that is good enough to match all your tags, it's a simple matter of using preg_replace_callback to pass each matched bit of text to a function you have created. That function can then look at the matched text and determine how to generate that which will replace it (be it from instantiating an object, including some HTML files or simple PHP output). Once it has the replacement text, the function just returns it. preg_replace_callback does most of the hard work 
Here's the kind of thing you could do:
That code is far from polished
It supports multiple var="val" pairs in tags, and any alphanumerical engine tag names.
Does that help?
Here's the kind of thing you could do:
Code: Select all
$pat="/<engine:([a-z0-9]+)(\s+.+)?\/>/Ui";
preg_replace_callback ($pat, "doThis", $text);
function doThis($m) {
preg_match_all("/([a-z0-9]+)="(.*)"/Ui", $m[2], $match, PREG_SET_ORDER);
$return="";
foreach ($match as $pair) {
list($var, $val)=$pair;
// Work out what to return
}
return $return;
}Does that help?
-
fastfingertips
- Forum Contributor
- Posts: 242
- Joined: Sun Dec 28, 2003 1:40 am
- Contact:
Now i'm at daily work but in the evening i will gladly try your solution, if this will work i will try to publish my arhitecture.
I'm trying to develop a content management system that will allow also to a beginner to create and manage a website. If this will be working in the what i named public features (some default functiones needed in every site) i will try to develop more advanced features when i will end it i will publish my work.
I'm trying to develop a content management system that will allow also to a beginner to create and manage a website. If this will be working in the what i named public features (some default functiones needed in every site) i will try to develop more advanced features when i will end it i will publish my work.