XML to Objects in PHP 5.x eregi_replace vs preg_replace
Posted: Sat Feb 13, 2016 6:13 pm
Wow, so I just modified my XML reader and Object nesting creator from eregi_replace to preg_replace. It went from confusing to what the fuck am I doing to oh... this is much more logical and I can understand it.
I built my own CMS website skeletons like wordpress in a way. So, instead of always using MySQL for a db and tables, sometimes I take the route of text files sites.xml and interests.xml etc
At times, I might forget to add no more than one space, or tabs and newlines all over the place to make my code easier to understand. Well, when you parse, or mechanically read it, the computer goes nuts. 7 years ago I tackled making XML into an object so you could access it with arrows in a family tree in PHP
db-> sect-> (title | blurb | gallery | link)
db[0]->sect[0]->title[0]-DATA
would be the title from
<db>
<sect>
<title>the mummy</title>
</sect>
</db>
etc..
well, regular expression are NUTS! Especially the 40+ flavors where the rules are always all different. So, I tackled POSIX a long time ago, which is eregi_replace in PHP, and now a more logical and expressive version PCRE.
What once was
"find a greater than symbol >"
"find lots of space, tabs, or new lines"
"find a less than symbol <"
in POSIX was this:
$data=eregi_replace(">"."[[:space:]]+"."<","><",$data);
in PCRE is this:
$exp1='/(?<=>)\s+(?=<)/';
$data=preg_replace($exp1,'',$data);
and to then find "&" or "&" which are ampersands in two ways from a web server output, you could do this:
POSIX
$data=eregi_replace("&","##",$data);
PCRE
$data=html_entity_decode($data);
$exp2='/\&+/';
$data=preg_replace($exp2,'##',$data);
so now I can replace "&" and or "&" to just "&" inline with a cool "html_entity_decode" subrountine built into PHP
then I can find all "&" characters and replace them with '##' which I use to notate an ampersand just in case an xml field has & in it "LAW & ORDER" etc, looks like "LAW ## ORDER"
then when I spit it back into a DATA node I build in my nesting, I just tell it, hey replace it back from ## to &
POSIX
$data=eregi_replace("##","&",$data);
PCRE
$exp1='/(##)/i';
$data=preg_replace($exp1,'&',$data);
XML_parse.php
example_test_xml.php
sections_web.xml
I built my own CMS website skeletons like wordpress in a way. So, instead of always using MySQL for a db and tables, sometimes I take the route of text files sites.xml and interests.xml etc
At times, I might forget to add no more than one space, or tabs and newlines all over the place to make my code easier to understand. Well, when you parse, or mechanically read it, the computer goes nuts. 7 years ago I tackled making XML into an object so you could access it with arrows in a family tree in PHP
db-> sect-> (title | blurb | gallery | link)
db[0]->sect[0]->title[0]-DATA
would be the title from
<db>
<sect>
<title>the mummy</title>
</sect>
</db>
etc..
well, regular expression are NUTS! Especially the 40+ flavors where the rules are always all different. So, I tackled POSIX a long time ago, which is eregi_replace in PHP, and now a more logical and expressive version PCRE.
What once was
"find a greater than symbol >"
"find lots of space, tabs, or new lines"
"find a less than symbol <"
in POSIX was this:
$data=eregi_replace(">"."[[:space:]]+"."<","><",$data);
in PCRE is this:
$exp1='/(?<=>)\s+(?=<)/';
$data=preg_replace($exp1,'',$data);
and to then find "&" or "&" which are ampersands in two ways from a web server output, you could do this:
POSIX
$data=eregi_replace("&","##",$data);
PCRE
$data=html_entity_decode($data);
$exp2='/\&+/';
$data=preg_replace($exp2,'##',$data);
so now I can replace "&" and or "&" to just "&" inline with a cool "html_entity_decode" subrountine built into PHP
then I can find all "&" characters and replace them with '##' which I use to notate an ampersand just in case an xml field has & in it "LAW & ORDER" etc, looks like "LAW ## ORDER"
then when I spit it back into a DATA node I build in my nesting, I just tell it, hey replace it back from ## to &
POSIX
$data=eregi_replace("##","&",$data);
PCRE
$exp1='/(##)/i';
$data=preg_replace($exp1,'&',$data);
XML_parse.php
Code: Select all
<?php
/*
XML_parse.php v3.0 generic xml parser to associate arrays ©2008,2016 by Kristoffe Brodeur. All Rights Reserved.
04-07-2008 COMPACT VERSION v1.0
08-24-2008 added CSS formatting and colored DIV alternations
08-28-2008 found a way to replace &->## and then ##->& so the PHP parser wouldn't freak out for HTML links with variables
02-16-2009 made XML_parse.php it's own file to be included in other php files by include 'XML_parse.php';
02-23-2009 added __contruct php5 class model changes
07-01-2009 made all calls have proper amounts of arguments
07-14-2009 found that if(!tgtPar->{$sName}) throws an error but if(!isset(tgtPar->{$sName}) does not, thus the apache error.log fills up slower!
02-13-2016 preg_replace is depreciated, so now preg_replace is used.
reformatted code layout
/(?<=>)\s+(?=<)/ replace all >..~..< with space(s) in-between with just >< by selecting inside > and < and giving '' nothing
html_entity_decode change all forms of "&" or "&" with "&"
'/\&+/ replace all "&" with "##" for coding and non collision with xml
/(##)/i inline into an object oriented format, replace all "##" with "&"
*/
//
Class Node
{
var $ID;
var $DATA;
//
function __construct($sID,$sText)
{
$this->ID=$sID;
if($sText!=""){$this->DATA=$sText;}
else{$this->DATA="-";}
}
}
//
Class NodeArray
{
//
function __construct()
{
}
}
//
function AddNode($tgtPar,$sName,$sText)
{
//
if(!isset($tgtPar->{$sName}))
{
$tgtPar->{$sName}=Array();
}
$ptr=count($tgtPar->{$sName});
$tgtPar->{$sName}[$ptr]=new Node($sName,$sText);
}
//
Class XML_parse
{
var $fName;
var $depth;
var $nest;
var $nodes;
var $XML;
var $tgt;
//
function __construct($sFName)
{
$this->nest=array();
$this->depth=-1;
$this->fName=$sFName;
$this->XML=new NodeArray(null);
//
if(!($this->xmlparser=xml_parser_create()))
{
die("Cannot create parser.");
}
else
{
echo "Xml parser created!<br />";
}
xml_set_object($this->xmlparser,$this);
xml_set_element_handler($this->xmlparser,"start_tag","end_tag");
xml_set_character_data_handler($this->xmlparser,"tag_contents");
//
if(!($fp=fopen($this->fName,"r")))
{
die("cannot open ".$this->fname);
}
while($data=fread($fp,4096))
{
$exp1='/(?<=>)\s+(?=<)/';
$data=preg_replace($exp1,'',$data);
$data=html_entity_decode($data);
$exp2='/\&+/';
$data=preg_replace($exp2,'##',$data);
echo "data|$data";
//
if(!xml_parse($this->xmlparser,$data,feof($fp)))
{
$reason=xml_error_string(xml_get_error_code($this->xmlparser));
$reason.=xml_get_current_line_number($this->xmlparser);
die($reason);
}
}
}
//
function addCurrData($sData)
{
$pNode=$this->XML;
$cnt=count($this->nest);
//
for($a1=0;$a1<$cnt;$a1++)
{
$ptr=$this->nest[$a1];
$pNodeCnt=count($pNode->{$ptr});
$pNode=$pNode->{$ptr}[$pNodeCnt-1];
}
$pNode->DATA=$sData;
}
//
function createNode($sNode)
{
$pNode=$this->XML;
//
if($this->depth==0)
{
}
else
{
$cnt=count($this->nest);
//
for($a1=0;$a1<$cnt-1;$a1++)
{
$ptr=$this->nest[$a1];
$pNodeCnt=count($pNode->{$ptr});
$pNode=$pNode->{$ptr}[$pNodeCnt-1];
}
}
AddNode($pNode,$sNode,null);
$pCnt=count($pNode->{$sNode});
}
//
function start_tag($parser,$name,$attribs)
{
$lNode=strtolower($name);
$this->depth++;
array_push($this->nest,$lNode);
$this->createNode($lNode);
$cnt=count($attribs);
$tab=$this->tabit($this->depth);
}
//
function end_tag($parser,$name)
{
$tab=$this->tabit($this->depth);
$lNode=strtolower($name);
$this->depth--;
array_pop($this->nest);
}
//
function tag_contents($parser,$data)
{
$exp1='/(##)/i';
$data=preg_replace($exp1,'&',$data);
$this->addCurrData($data);
//echo '<br />['.$data.']';
}
//
function showNest()
{
$nL=count($this->nest);
//
for($a1=0;$a1<$nL;$a1++)
{
echo "[".$this->nest[$a1]."]";
}
}
//
function tabit($amt)
{
$str="";
$chr=" ";
//
for($a1=0;$a1<$amt;$a1++)
{
$str.=$chr;
}
return $str;
}
}
?>
Code: Select all
<?php
require "XML_parse.php";
$filename="sections_web.xml";
$XML1=new XML_parse($filename);
$tgt=$XML1->XML->db[0]->sect;
$lenT=count($tgt);
echo "($len)";
//
for($t=0;$t<$lenT;$t++)
{
$strS=$tgt[$t]->title[0]->DATA;
echo "<h4>$strS</h4>";
}
$XML1->showNest();
?>
Code: Select all
<db>
<sect>
<title>Philadelphia Festival Pier</title>
<blurb>A custom scripted wordpress theme and content site for Philadelphia Festival Pier. Reprogrammed widgets in PHP and MySQL with CSS dynamically scripted to sit on top of theme 'twenty-ten' and use nivo slider re-scripted to a different size, headers and posts dynamically resorted with custom tags. 4 column performance list dynamically rebuilt to even out the lengths of each column. Art and layout custom made as well.</blurb>
<gallery>festival</gallery>
<link>http://www.festivalpierphilly.com</link>
</sect>
<sect>
<title>Vox Inc.Design Consortium</title>
<blurb>This multimedia website has a custom designed PHP GD2 drop down menu system. The client wanted to have a transparent custom TTF typeface and animate it, but not rely entirely on flash. The menu system was broken into segments, and a dynamic backend renderer was created for the menu system to make transparent PNG files rendered via GD2, which can change anytime the client modifies the areas in the backend. There is jQuery, JS, PHP and AS2 all working together to get the custom look they required.</blurb>
<gallery>vox</gallery>
<link>http://www.vox-inc.net</link>
</sect>
<sect>
<title>Advance Remodelling & Construction</title>
<blurb>A custom feedback module was created for this site that randomizes the feedback for a unique visit every time the pages are refreshed in relation to the feedback db. There is also a GD2 captcha system written to block bot spam and hacker attacks. An interactive gallery with a flash pop up menu and backend for loading was created for easy client modification.</blurb>
<gallery>advance</gallery>
<link>http://72.94.171.34:8080/advance/index.php</link>
</sect>
<sect>
<title>Aikido of Philadelphia</title>
<blurb>This site is a constantly changing experimental site from flash only to modular flash and php CMS backend creation including multiple MySQL backend areas such as gallery manipulation, event creation and calendar access/modification. Multiple custom flash animations (machines) were created for the intro screens, class schedules, and video gallery.</blurb>
<gallery>aikido</gallery>
<link>http://www.aikidophiladelphia.org</link>
</sect>
<sect>
<title>The Academy of Social Dance</title>
<blurb>A flash site that could be modifiable was created for this client, so the aspects of traditional plug and play html design was grouped with flash modules per page. A custom music jukebox was created with the ability to simply add mp3 files and the player automatically updates live on the site for all listeners.</blurb>
<gallery>academy</gallery>
<link>http://academyofsocialdance.com</link>
</sect>
<sect>
<title>Lakota Sioux Dance Theatre</title>
<blurb>The site was created to diplay the media arts of the Lakota Sioux and to hold live promotional materials burnable and/or printable globally. The bio section is a hybrid of as2 to as3 with a simple XML engine created for the quick loading and manipulation of the data in the backend.</blurb>
<gallery>lakota</gallery>
<link>http://www.lakotadancetheatre.org</link>
</sect>
<sect>
<title>Max Kleen Carpet</title>
<blurb>This is a straight forward site with a custom coupon database system and backend in mysql and php editable bodycopy via mysql. The dynamic menu system for areas is done also in mysql.</blurb>
<gallery>maxkleen</gallery>
<link>http://www.maxkleencarpet.com</link>
</sect>
<sect>
<title>Liberty Receivables</title>
<blurb>This is an online brochure and promotional piece for a collection agency with flash modules.</blurb>
<gallery>liberty</gallery>
<link>http://www.libertyreceivables.com</link>
</sect>
<sect>
<title>Professional Women's Roundtable</title>
<blurb>This site needed a cross browser compatible drop down menu system with z-depth in javascript. The site was designed by my friend John Walton and I built the programming.</blurb>
<gallery>pwr</gallery>
<link></link>
</sect>
<sect>
<title>World Festival of Dinosaurs</title>
<blurb>This is the frontend to a promotional video and virtual reality space for both the United Arab Emirates to make a selling point for the world's largest dinosaur museum and entertainment/education center in the world. All 3d modeling was created from scratch and the architecture was taken from simple thumbnails provided. The video was edited and inserted into a custom media player in flash, and the introduction dinosaurs are original artwork from a web search.</blurb>
<gallery>wfd</gallery>
<link>http://www.worldfestivalofdinosaurs.com</link>
</sect>
<sect>
<title>Neurocepter</title>
<blurb>This is a headset design created by myself, Kristoffe, for a promotion to Neurocepter. It controls the mind by using brain wave patterns sensed by a third party algorithm, and the piece was a simple flash and html design, along with the Industrial Design and research of the product itself.</blurb>
<gallery>neurocepter</gallery>
<link>http://www.neurocepter.com</link>
</sect>
<sect>
<title>CC3D Architectural Renderings</title>
<blurb>A traditional artist needed a site to promote his art, and it was created completely in flash, but gave it an html kiosk feel.</blurb>
<gallery>cc3d</gallery>
<link>http://www.cc3d.com</link>
</sect>
<sect>
<title>Brian Bailey 71</title>
<blurb>This was created for a professional sportscaster for a variety of sports and events. It was a hybrid of html and flash with a custom mp3 player for the sportscasting mp3 streams.</blurb>
<gallery>bb71</gallery>
<link>http://www.brianbailey71.com</link>
</sect>
<sect>
<title>Vandelay Marketing</title>
<blurb>A commercial site frontend for an internet vendor with an existing .NET backend.</blurb>
<gallery>vandelay</gallery>
<link>http://www.vandelaymarketing.com</link>
</sect>
</db>