Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Php regular expression braincracker

8:58 pm on Aug 27, 2008 (gmt 0)

New User

5+ Year Member

joined:Aug 27, 2008
posts: 1
votes: 0

Hi all,

I wrote a abstract layer for my database objects but recently I bumped into a problem which I'm not able to solve up till now.
I'm not searching for an easy solution here, but maybe someone can point me in the right direction.
For parsing of template generated content I need some expressions but I can't formulate them correctly.
Basically, what I want to do is to fetch certain variables from html code and replace them by values stored in the database. I seems easy but the expression for it is much more tricky than one would think.

Suppose you have the following html string :

$html = '<datagrid name="Author">'.
'<field name="FirstName"/>'.
'<field name="LastName"/></h1><br />'.
'<one-to-many name="Book">'.
'Books written : <br />'.
'Title : <field name="Title"/><br />'.
'Type : <field name="Type"/><br />'.
'Details : <br />'.
'<one-to-many name="Page">'.
'Page <field name="Number"/> : <br />'.
'Content <field name="Content"/>'.
'<hr><br />'.
'<datagrid name="MusicSongs">'.
'<field name="Title"/> - <field name="Album"/>'.
'<br />'.
'<p>Author birthdate : <field name="BirthDate"/></p>'.

I need to fetch all datagrid, field and one-to-many tags, including their attributes, parse them to a php DAO object and send it to my backend server. The Dao object looks like this :

class DaoType extends Object {

var $className;
var $fields = array();
var $oneToMany = array();

function __construct(){}

function getClassName(){
return $this->className;

function setClassName($className){
$this->className = $className;

function getFields(){
return $this->fields;

function setFields($fields){
$this->fields= $fields;

function getOneToMany(){
return $this->oneToMany;

function setOneToMany($oneToMany){
$this->oneToMany = $oneToMany;

Please note the following issues :

  1. datagrid is the super tag. All field and one-to-many tags must be inside a datagrid tag. A field tag however can be inside a one-to-many tag. A one-to-many tag can be nested in another one-to-many.
  2. All field and one-to-many tags should register inside their first parent tag DAO object. (datagrid or one-to-many)
  3. A field tag is a closed tag. Is may not contain any content and must be closed immediately. datagrid and one-to-many tags must contain at least one field or one-to-many tag.
  4. datagrid tags can be nested. In this case, we should parse a DAO object for each datagrid tag. Also note that content of the inner datagrid shouldn't be added to the outer datagrid DAO object.
  5. There's no predefined sequence for field and one-to-many tags. Fe the field author->birthdate comes after the one-to-many book tag. It should be registered however as a field in the upper Author DAO.
  6. one-to-many tags can be nested unlimited
  7. There's absolutely no relationship between different datagrids although they can be nested.

For example, the html string above should result in two DaoType objects:

The inner datagrid MusicSongs is a DAO with following properties :

$className = MusicSongs
$fields = array("Title", "Album")
$oneToMany = array()

The outer datagrid Author is a DAO with following properties :

$className = Author
$fields = array("FirstName", "LastName", "BirthDate")
$oneToMany = array($BookDaoObject)

The BookDaoObject above is again a DAO object with following properties :

$className = Book
$fields = array("Title", "Type")
$oneToMany = array($PageDaoObject)

The PageDaoObjectabove is again a DAO object with following properties :

$className = Page
$fields = array("Number", "Content")
$oneToMany = array()

So what I need is a preg_match expression to fetch all datagrids from inside to outer and separate their content (= html + fields + onetomany tags).
Once the datagrids are separated, we need to do search for all fields and onetomany (nested!). onetomany must be registered as a new DAO object inside it's parent container so we obtain the object structure as above.

I know one can do neat and powerfull stuff with these regular expressions, and it must be possible. Unfortunately, I'm not an expert in this area. It caused already several serious headaches, but I can't manage to solve this one. Maybe somebody has any suggestions? Maybe a different approach will bring light in the dark for me.

Anyhow, any help will be highly appreciated.

Kind regards,


9:33 pm on Aug 27, 2008 (gmt 0)

Full Member

10+ Year Member

joined:Feb 4, 2004
votes: 0

You may want to take a look at PHPs XML Parser [php.net] functions. I've never used them, but I think you could use them for your application.

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members