Need a parser idea

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Need a parser idea

Post 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?
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Why not just use XML? Then you've got a parser built-in and a templating system (XSL) at your disposal!
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post 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.
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post 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.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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...
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post 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?
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

The XML parser is not experimental - you must be reading that from old documentation. Only the DOMXML functions are experimental.
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post 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?
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post 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.
Post Reply