Page 1 of 1

Need a parser idea

Posted: Thu Jun 03, 2004 9:52 am
by fastfingertips
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?

Posted: Thu Jun 03, 2004 10:27 am
by launchcode
Why not just use XML? Then you've got a parser built-in and a templating system (XSL) at your disposal!

Posted: Fri Jun 04, 2004 4:37 am
by fastfingertips
Because of this
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.
Also because the code behind at what i'm thinking should look like:
<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.

Posted: Fri Jun 04, 2004 5:17 am
by dave420
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.

Posted: Fri Jun 04, 2004 5:34 am
by patrikG
fastfingertips wrote:Because of this
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.
Also because the code behind at what i'm thinking should look like:
<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.
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.
no need to re-invent the wheel - unless you're hellbent on doing that anyway...

Posted: Fri Jun 04, 2004 5:55 am
by fastfingertips
Dave quite amasing idea from you, i will do that using your idea.
Can you provide me some tips based on your experience?

Posted: Fri Jun 04, 2004 7:40 am
by launchcode
The XML parser is not experimental - you must be reading that from old documentation. Only the DOMXML functions are experimental.

Posted: Mon Jun 07, 2004 3:06 am
by dave420
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:

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;
}
That code is far from polished :) It supports multiple var="val" pairs in tags, and any alphanumerical engine tag names.

Does that help?

Posted: Mon Jun 07, 2004 3:16 am
by fastfingertips
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.