Forum Moderators: coopster
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">'.
'<h1>'.
'<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"/>'.
'</one-to-many>'.
'</one-to-many>'.
'<hr><br />'.
'<datagrid name="MusicSongs">'.
'<field name="Title"/> - <field name="Album"/>'.
'<br />'.
'</datagrid>'.
'<p>Author birthdate : <field name="BirthDate"/></p>'.
'</datagrid>';
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 :
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,
wapsi