Although smarty may be a good bet (why reinvent the wheel for the 14th time?), The etranger may have a point. There are situations in which you do not want to use something overkill, like a complete new (and unexplored) package, but rather that you want to publish some data from a database that just contains the html files that XML encountered parser.
A warning. The following is a simple solution, but do not do it if you are NOT SURE that you can get away from it! (I did this when I had about 2 hours before the deadline and didnโt have time to study, leave a lone tool of something like smart ...)
Before inserting a string into the appendXML function, run it through preg_replace. For example, replace all and nbsp; characters with [some_prefix] _nbsp. Then, on the page where you display html, do the opposite.
And Presto! =)
Code example: Code that places text in a document fragment:
// add text tag to p tag. // print("CCMSSelTextBody::getDOMObject: strText: ".$this->m_strText."<br>\n"); $this->m_strText = preg_replace("/ /", "__nbsp__", $this->m_strText); $domTextFragment = $domDoc->createDocumentFragment(); $domTextFragment->appendXML(utf8_encode($this->m_strText)); $p->appendChild($domTextFragment); // $p->appendChild(new DOMText(utf8_encode($this->m_strText)));
Code that parsed the string and wrote html:
// Instantiate template. $pTemplate = new CTemplate($env, $pageID, $pUser, $strState); // Parse tag-sets. $pTemplate->parseTXTTags(); $pTemplate->parseCMSTags(); // present the html code. $html = $pTemplate->getPageHTML(); $html = preg_replace("/__nbsp__/", " ", $html); print($html);
It's probably a good idea to come up with a stronger replacement. (If you insist on being careful: do md5 at time (), and hardcode is the result of this as a prefix. So in the first snippet:
$this->m_strText = preg_replace("/ /", "4597ee308cd90d78aa4655e76bf46ee0_nbsp", $this->m_strText);
And in the second:
$html = preg_replace("/4597ee308cd90d78aa4655e76bf46ee0_nbsp/", " ", $html);
Do the same for any other tags and things you need to get around.
This is a hack, not a good code for any part of the imagination. But it saved my life and wanted to share it with other people who are faced with this problem with minimal time.
Use the above at your own risk.