tag:blogger.com,1999:blog-3123008930619642972009-07-09T03:40:06.543-07:00My SRC<b><a href="http://mysrc.blogspot.com/2007/02/php-xml-to-array-and-backwards.html">PHP XML Parser</a></b>leoSrhttp://www.blogger.com/profile/09731107791619864392noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-312300893061964297.post-92226858419023706172007-02-14T05:26:00.000-08:002007-02-15T05:02:34.834-08:00PHP: Form with smarty + captchaForm composition with smarty + captcha<br /><br /><pre class=php id=php1><br />/*<br /> Usage: <br /> $ff=array(<br /> array('text', 'field_name', 'label', array('class'=&gt;'area'), array('postlabel'=&gt;'ddd', 'validate'=&gt;'empty')),<br /> array('separator', 'td_class', 'text here'),<br /> array('options', 'field_name2', 'label', array('options'=&gt;array(1=&gt;2)), array('postlabel'=&gt;'ddd', 'check'=&gt;'empty')),<br /> array('submit', '', '', array('value'=&gt;'Sign up')),<br /> array('hidden', 'hid', '', array('value'=&gt;'blah'))<br /> );<br /> sf_init($ff);<br /> <br /> $smarty-&gt;assign(array('f2'=&gt;$ff, 'in'=&gt;$_REQUEST));<br /> $sm-&gt;register_function('smarty_form', 'smarty_form');<br /> {smarty_form fields=$f2 in=$in table_attr=&quot;class=tabt cellpadding=0 cellspacing=0&quot; form_attr=&quot;method=post&quot; colspan_class=&quot;separator&quot;}<br /><br />*/<br /><br />function sf_init(&amp;$ff) {<br /> $ft=array('type', 'name', 'label', 'fp', 'gp'); // make readable array<br /> foreach ($ff as $fd) {<br /> $fdh=array(); foreach ($ft as $i=&gt;$t) {if (isset($fd[$i])) $fdh[$t]=$fd[$i];}<br /> $f2[]=$fdh;<br /> }<br /> $ff=$f2;<br />}<br />function iname($f2, $fd) {<br /> foreach ($f2 as $i=&gt;$f) {if ($f['name']==$fd) $fi=$i;}<br /> return (isset($fi) ? $fi : -1);<br />}<br />function vfields($f2) {<br /> $fr=array();<br /> foreach ($f2 as $f) {<br /> if (!in_array($f['type'], array('separator', 'submit')) &amp;&amp; $f['name']!='captcha') $fr[]=$f['name']; <br /> }<br /> return $fr;<br />}<br /><br />function smarty_form($p, &amp;$smarty) {<br /> $hidden=array();<br /> foreach ($p['fields'] as $i=&gt;$_fd) {$fd=&amp;$p['fields'][$i];<br /> // start values <br /> $fd['fp']['name']=$fd['name']; // for form tags<br /> $fd['fp']['type']=$fd['type'];<br /> if (isset($p['in'][$fd['name']])) { // in-value<br /> $par=(in_array($fd['type'], array('options', 'radios', 'checkboxes')) ? 'selected' : 'value');<br /> $fd['fp'][$par]=$p['in'][$fd['name']];<br /> }<br /> // generate fields<br /> if (in_array($fd['type'], array('options', 'radios', 'checkboxes'))) {<br /> require_once(SMARTY_DIR.&quot;plugins/function.html_$fd[type].php&quot;);<br /> if (isset($p['in'][$fd['name']])) $fd['fp']['selected']=$p['in'][$fd['name']];<br /> $fields_t[$fd['name']]=call_user_func(&quot;smarty_function_html_$fd[type]&quot;, $fd['fp'], $smarty);<br /> } elseif (!in_array($fd['type'], array('separator', 'hidden'))) {<br /> $fields_t[$fd['name']]=_field_gen($fd['fp'], $fd['type']);<br /> }<br /> // data for table, hidden fields<br /> if ($fd['type']=='hidden') $hidden[]=_field_gen($fd['fp'], $fd['type']);<br /> elseif ($fd['type']=='separator') $data[][]=$fd['label'];<br /> else $data[]=array(<br /> $fd['label'].(isset($fd['gp']['check']) ? ' *' : ''), <br /> $fields_t[$fd['name']].(isset($fd['gp']['plabel']) ? $fd['gp']['plabel'] : '') <br /> );<br /> }<br /> <br /> $js=_jstext($p['fields']); // javascript-check<br /> return $js.'&lt;form '.$p['form_attr'].($js ? ' onsubmit=&quot;return validate_form(this)&quot;' : '').'&gt;'.<br /> &quot;\n&quot;.implode(&quot;\n&quot;, $hidden).&quot;\n&quot;._tablefm($data, &amp;$p).'&lt;/form&gt;';<br />}<br />function _tablefm($data, $p) {<br /> $res[]='&lt;table '.$p['table_attr'].'&gt;';<br /> $cols=0; foreach ($data as $line) {$c=count($line); if ($c&gt;$cols) $cols=$c;}<br /> foreach ($data as $line) {<br /> $res[]='&lt;tr&gt;';<br /> if (count($line)==1) $res[]=&quot;&lt;td class=\&quot;$p[colspan_class]\&quot; colspan=$cols&gt;$line[0]&lt;/td&gt;&quot;;<br /> else {foreach ($line as $y=&gt;$v) $res[]=&quot;&lt;td&gt;$v&lt;/td&gt;&quot;;}<br /> $res[]='&lt;/tr&gt;';<br /> }<br /> $res[]='&lt;/table&gt;';<br /> return implode(&quot;\n&quot;, $res);<br />}<br />function _jstext($fields_h) {<br /> foreach ($fields_h as $fd) {<br /> if (!isset($fd['gp']['check'])) continue;<br /> $rs['name'][]=$fd['name'];<br /> $rs['label'][]=$fd['label'];<br /> $rs['check'][]=$fd['gp']['check'];<br /> }<br /> if (!$rs) return ''; <br /> return '<br />&lt;script&gt;<br />v_names = new Array(&quot;'.implode('&quot;, &quot;', $rs['name']).'&quot;);<br />v_checks = new Array(&quot;'.implode('&quot;, &quot;', $rs['check']).'&quot;);<br />v_labels = new Array(&quot;'.implode('&quot;, &quot;', $rs['label']).'&quot;);<br /><br />function validate_form(obj) {<br /> errors=&quot;&quot;; <br /> for (i=0; i&lt;obj.elements.length; i++) {<br /> for (y=0; y&lt;v_names.length; y++) {<br /> if (obj.elements[i].name==v_names[y]) errors+=_validate(v_checks[y], obj.elements[i], v_labels[y]);<br /> }<br /> }<br /> if (errors) {alert(errors); return false;}<br /> else return true; <br />}<br />function _validate(type, obj, label) {<br /> res=&quot;&quot;; <br /> if (type==&quot;email&quot;) {<br /> if (obj.value.indexOf(&quot;.&quot;)&gt;2 &amp;&amp; obj.value.indexOf(&quot;@&quot;) &gt; 0) res=&quot;&quot;;<br /> else res=&quot;E-mail address is not valid\n&quot;;<br /> } else if (type==&quot;empty&quot;) {<br /> if (obj.value==&quot;&quot; || obj.value==null) res=&quot;Field \&quot;&quot;+label+&quot;\&quot; is empty\n&quot;;<br /> }<br /> return res;<br />}<br />&lt;/script&gt;<br />';<br />}<br />function _field_gen($params, $type='input') {<br /> if ($type=='textarea') {<br /> foreach ($params as $k=&gt;$v) {if ($k!='value') $pp=&quot;$k=\&quot;$v\&quot;&quot;;}<br /> return '&lt;textarea '.implode(' ', $pp).'&gt;'.(isset($params['value']) ? $params['value'] : '').'&lt;/textarea&gt;';<br /> } else {<br /> foreach ($params as $k=&gt;$v) {$pp[]=&quot;$k=\&quot;$v\&quot;&quot;;}<br /> return '&lt;input '.implode(' ', $pp).'&gt;';<br /> }<br />}<br /> <br /><br />/* <br /> Usage:<br /> image.php: <br /> $captcha=array(140, 34, 'britanic.ttf', 25, 0, 0, 0, array(255,255,255), array(0,0,50)); <br /> captcha();<br /> form.php: <br /> &lt;form method=&quot;POST&quot;&gt;&lt;img src=&quot;img.php&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;captcha&quot;&gt;&lt;input type=&quot;submit&quot;&gt;&lt;/form&gt;<br /> if ($_POST &amp;&amp; check_captcha()) echo &quot;ok&quot;;<br /> <br />*/<br /><br />function captcha() {<br /> global $captcha;<br /> list($box_w, $box_h, $font, $font_size, $font_angle, $margin_left, $margin_top, $color_bg, $color_text)=$captcha;<br /> $pass=''; $chm=rand(0,1);<br /> $ch=array(str_split('aeiouy'), str_split('bcdfjhgkmnprstvxz'));<br /> while (strlen($pass)&lt;6) {$pass.=$ch[$chm][array_rand($ch[$chm])]; $chm=($chm ? 0:1);}<br /> session_start();<br /> $_SESSION['captcha'] = $pass;<br /> $image = ImageCreatetruecolor($box_w,$box_h); // create the image resource<br /> $color_bg = ImageColorAllocate($image, $color_bg[0], $color_bg[1], $color_bg[2]); // set colors<br /> $color_text = ImageColorAllocate($image, $color_text[0], $color_text[1], $color_text[2]);<br /> imagefill($image, 0, 0, $color_bg); // set background<br /> imagettftext($image, $font_size, $font_angle, $margin_left, $font_size + $margin_top, $color_text, $font, $pass); // set text<br /> header(&quot;Content-Type: image/jpg&quot;); // or image/jpg<br /> imagejpeg($image); // created image<br /> imagedestroy($image);<br />}<br />function check_captcha() {<br /> session_start();<br /> if (isset($_SESSION['captcha']) &amp;&amp; isset($_REQUEST['captcha']) &amp;&amp; $_SESSION['captcha']==$_REQUEST['captcha']) {<br /> unset($_SESSION['captcha']);<br /> return 1;<br /> }<br /> return 0;<br />}<br /></pre><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/312300893061964297-9222685841902370617?l=mysrc.blogspot.com'/></div>leoSrhttp://www.blogger.com/profile/09731107791619864392noreply@blogger.com0tag:blogger.com,1999:blog-312300893061964297.post-9811338644336482832007-02-14T00:57:00.000-08:002007-03-14T01:29:37.780-07:00PHP highlight in JavascriptJavascript <br /><br /><pre class=php id=js1><br />&lt;script&gt;<br />function h_js(name) {<br /> h_base(name, &quot;js_&quot;, &quot;(function|var|null|false|true|try|catch|if|else|new|return|for)&quot;, &quot;([\!\(\)=\}\{,.])&quot;);<br />}<br />function h_php(name) {<br /> h_base(name, &quot;php_&quot;, &quot;(function|global|array|echo|if|else|elseif|return|foreach|as|[\!\(\)=\}\{,.])&quot;);<br />}<br />function h_java(name) {<br /> h_base(name, &quot;java_&quot;, &quot;(package|import|public|private|static|final|class|void|null|boolean|int|false|true|try|catch|if|else|new|return|for)&quot;);<br />}<br />function h_base(name, wpre, bvar, bres) {<br /> obj=document.getElementById(name); if (!obj) return;<br /> bvar=new RegExp(bvar, &quot;ig&quot;); <br /> bquote=new RegExp(&quot;(\&quot;[^\&quot;]+\&quot;|'[^']+')&quot;, &quot;ig&quot;); <br /> bcomm=new RegExp(&quot;(\/\/ .*)&quot;, &quot;ig&quot;); <br /> if (bres) bres=new RegExp(bres, &quot;ig&quot;);<br /> ta=obj.innerHTML.split(lnbr);<br /> for (i=0; i &lt; ta.length; i++) {<br /> cl=wpre+&quot;res&quot;; if (bres) ta[i]=ta[i].replace(bres, &quot;&lt;font class=&quot;+cl+&quot;&gt;$1&lt;/font&gt;&quot;);<br /> cl=wpre+&quot;var&quot;; ta[i]=ta[i].replace(bvar, &quot;&lt;font class=&quot;+cl+&quot;&gt;$1&lt;/font&gt;&quot;);<br /> cl=wpre+&quot;quote&quot;; ta[i]=ta[i].replace(bquote, &quot;&lt;font class=&quot;+cl+&quot;&gt;$1&lt;/font&gt;&quot;);<br /> cl=wpre+&quot;comment&quot;; ta[i]=ta[i].replace(bcomm, &quot;&lt;font class=&quot;+cl+&quot;&gt;$1&lt;/font&gt;&quot;);<br /> }<br /> tj=ta.join(&quot;_LN_&quot;); cl=wpre+&quot;comment&quot;; tj=tj.replace(/(\/\*.*?\*\/)/gi, &quot;&lt;font class=&quot;+cl+&quot;&gt;$1&lt;/font&gt;&quot;); ta=tj.split(&quot;_LN_&quot;); <br /> cl=wpre+&quot;in&quot;; obj.innerHTML=&quot;&lt;pre class=&quot;+cl+&quot;&gt;&quot;+ta.join(&quot;\n&quot;)+&quot;&lt;/pre&gt;&quot;;<br />}<br />&lt;/script&gt;<br /></pre><br /><br />change code to push it to blog<br /><br /><pre class=php id=php1><br />function out_html($fname) {<br /> $tt=htmlentities(file_get_contents($fname));<br /> $tt=str_replace("\t", " ", $tt);<br /> echo $tt;<br />}<br />function js_esc($fname) {<br /> $txt=file_get_contents('1.txt');<br /> $txt=preg_replace('/\r?\n/', '', $txt);<br /> $txt=str_replace('\\', '\\\\', $txt);<br /> $txt=str_replace('&quot;', '\\&quot;', $txt);<br /> echo &quot;&lt;script&gt;document.write(escape(\&quot;$txt\&quot;));&lt;/script&gt;&quot;;<br />}<br /></pre><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/312300893061964297-981133864433648283?l=mysrc.blogspot.com'/></div>leoSrhttp://www.blogger.com/profile/09731107791619864392noreply@blogger.com0tag:blogger.com,1999:blog-312300893061964297.post-6750725648327901532007-02-07T01:52:00.000-08:002007-02-14T05:35:33.579-08:00PHP: MySQL interface<pre class=php id=php1><br />/*<br /> Mysql functions. Usage:<br /> <br /> $isloc=1; $db=array('loc'=&gt;array('localhost', 'root', '', 'db1'), 'srv'=&gt;array());<br /> mconn();<br /> msel(&quot;INSER2T INTO stats (^i) VALUES (^v)&quot;, array('dt'=&gt;'2006-12-25', 'hh'=&gt;'5'));<br /> msel(&quot;UPDATE stats SET ^u WHERE &quot;.mesc('id=%s', &quot;log'in&quot;), array('dt'=&gt;'2006-12-25', 'hh'=&gt;'5'));<br /> msel(&quot;SELECT FROM stats WHERE login=%s&quot;, array(&quot;log'in&quot;), 'hash');<br /> mdis();<br /><br /> mesc('id=%s', &quot;log'in&quot;); <br /> mesc('id=%s OR id=%s', array(&quot;log'in&quot;, &quot;p'wd&quot;)); <br /> mesc('id IN (%a)', array(&quot;log'in&quot;, &quot;p'wd&quot;)); <br />*/<br /><br />function mconn($link='') {<br /> global $isloc, $db;<br /> $t=$link.($isloc ? 'loc' : 'srv');<br /> $db['_l'][$link]=mysql_connect($db[$t][0], $db[$t][1], $db[$t][2]); <br /> mysql_select_db($db[$t][3], $db['_l'][$link]);<br />}<br />function mdis($link='') {<br /> global $db;<br /> mysql_close($db['_l'][$link]);<br />}<br />function msel($qu, $fv=array(), $st='', $link='', $sq='') {<br /> global $db;<br /> if (strpos($qu, 'SELECT')!==false) {<br /> $qu=mesc($qu, $fv);<br /> if ($sq) echo $qu.&quot;&lt;br&gt;\n&quot;;<br /> $res=mysql_query($qu, $db['_l'][$link]);<br /> if ($res===false) {echo '&lt;b&gt;'.$qu.mysql_error().&quot;&lt;/b&gt;&lt;br&gt;\n&quot;; return;}<br /> $mfa=($st=='scal' ? MYSQL_NUM : MYSQL_ASSOC);<br /> $lines=array(); while ($row=mysql_fetch_array($res, $mfa)) $lines[]=$row;<br /> if ($st=='scal') return ($lines ? $lines[0][0] : '');<br /> else if ($st=='hash') return ($lines ? $lines[0] : array());<br /> else return $lines;<br /> return $lines;<br /> }<br /> if (strpos($qu, 'INSERT')!==false) {<br /> $fds=array(); $vas=array();<br /> foreach ($fv as $f=&gt;$v) {$fds[]=$f; $vas[]=&quot;'&quot;.mysql_escape_string($v).&quot;'&quot;;}<br /> $qu=str_replace('^i', implode(', ', $fds), $qu);<br /> $qu=str_replace('^v', implode(', ', $vas), $qu);<br /> } else if (strpos($qu, 'UPDATE')!==false) {<br /> $vas=array();<br /> foreach ($fv as $f=&gt;$v) {$vas[]=$f.'='.&quot;'&quot;.mysql_escape_string($v).&quot;'&quot;;}<br /> $qu=str_replace('^u', implode(', ', $vas), $qu);<br /> }<br /> if ($sq) echo $qu.&quot;&lt;br&gt;\n&quot;;<br /> $res=mysql_query($qu, $db['_l'][$link]);<br /> if ($res===false) {echo '&lt;b&gt;'.mysql_error().&quot;&lt;/b&gt;&lt;br&gt;\n&quot;; return;}<br />}<br />function mesc($qu, $fv) {<br /> if (strpos($qu, '%s')!==false) {<br /> if (!is_array($fv)) return str_replace('%s', &quot;'&quot;.mysql_escape_string($fv).&quot;'&quot;, $qu);<br /> else {foreach ($fv as $v) $qu=preg_replace('/%s/', &quot;'&quot;.mysql_escape_string($v).&quot;'&quot;, $qu, 1);}<br /> } else if (strpos($qu, '%a')!==false) {<br /> foreach ($fv as $i=&gt;$v) $fv[$i]=&quot;'&quot;.mysql_escape_string($v).&quot;'&quot;;<br /> $str=implode(', ', $fv); if (!$str) $str=0;<br /> return str_replace('%a', $str, $qu);<br /> } else return $qu;<br />}<br /></pre><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/312300893061964297-675072564832790153?l=mysrc.blogspot.com'/></div>leoSrhttp://www.blogger.com/profile/09731107791619864392noreply@blogger.com0tag:blogger.com,1999:blog-312300893061964297.post-59262408858238595312007-02-06T23:29:00.000-08:002007-03-13T08:18:09.895-07:00PHP: XML to Array and backwardsHere the XML with PHP solution: XML->Array and Array->XML.<br />Work with it as with usual array.<br /><br />Format XML->Array<br />_c - children<br />_v - value<br />_a - attributes<br /><br />This is 1.1 :)<br /><br /><span style="font-weight: bold;">Example #1 (1.xml):</span><br /><pre class=code><br />&lt;ddd&gt;<br /> &lt;onemore dd=&quot;55&quot;&gt;<br /> &lt;tt&gt;333&lt;/tt&gt;<br /> &lt;tt ss=&quot;s1&quot;&gt;555&lt;/tt&gt;<br /> &lt;tt&gt;777&lt;/tt&gt;<br /> &lt;/onemore&gt;<br /> &lt;two&gt;sdf rr&lt;/two&gt;<br />&lt;/ddd&gt;<br /><br /></pre><br /><br />The code:<br /><pre class=code id=php1><br />$xml=xml2ary(file_get_contents('1.xml'));<br />print_r($xml); <br /></pre><br /><br />Here is the Array result:<br /><pre class=code id=php2><br />Array<br />(<br /> [ddd] => Array (<br /> [_c] => Array (<br /> [onemore] => Array (<br /> [_a] => Array (<br /> [dd] => 55<br /> )<br /> [_c] => Array (<br /> [tt] => Array (<br /> [0] => Array (<br /> [_v] => 333<br /> )<br /> [1] => Array (<br /> [_a] => Array (<br /> [ss] => s1<br /> )<br /> [_v] => 555<br /> )<br /> [2] => Array (<br /> [_v] => 777<br /> )<br /> )<br /> )<br /> )<br /> [two] => Array (<br /> [_v] => sdf rr<br /> )<br /> )<br /> )<br />)<br /></pre><br /><br /><span style="font-weight: bold;">Example #2: </span>(1.xml as described before)<br /><pre class=code id=php3><br />$xml=xml2ary(file_get_contents('1.xml'));<br />$xml['ddd']['_c']['twomore']=$xml['ddd']['_c']['onemore'];<br />$xml['ddd']['_c']['twomore']['_c']['tt'][0]['_v']='hello';<br />echo ary2xml($xml);<br /></pre><br /><br />Will output result:<br /><pre class=code><br />&lt;ddd&gt;<br /> &lt;onemore dd=&quot;55&quot;&gt;<br /> &lt;tt&gt;hello&lt;/tt&gt;<br /> &lt;tt ss=&quot;s1&quot;&gt;555&lt;/tt&gt;<br /> &lt;tt&gt;777&lt;/tt&gt;<br /> &lt;/onemore&gt;<br /> &lt;two&gt;sdf rr&lt;/two&gt;<br /> &lt;twomore dd=&quot;55&quot;&gt;<br /> &lt;tt&gt;hello&lt;/tt&gt;<br /> &lt;tt ss=&quot;s1&quot;&gt;555&lt;/tt&gt;<br /> &lt;tt&gt;777&lt;/tt&gt;<br /> &lt;/twomore&gt;<br />&lt;/ddd&gt;<br /><br /></pre><br /><br /><b>THE Sources: :)</b><br /><pre class=code id=php4><br />/*<br /> Working with XML. Usage: <br /> $xml=xml2ary(file_get_contents('1.xml'));<br /> $link=&amp;$xml['ddd']['_c'];<br /> $link['twomore']=$link['onemore'];<br /> // ins2ary(); // dot not insert a link, and arrays with links inside!<br /> echo ary2xml($xml);<br />*/<br /><br />// XML to Array<br />function xml2ary(&amp;$string) {<br /> $parser = xml_parser_create();<br /> xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);<br /> xml_parse_into_struct($parser, $string, $vals, $index);<br /> xml_parser_free($parser);<br /><br /> $mnary=array();<br /> $ary=&amp;$mnary;<br /> foreach ($vals as $r) {<br /> $t=$r['tag'];<br /> if ($r['type']=='open') {<br /> if (isset($ary[$t])) {<br /> if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array());<br /> $cv=&amp;$ary[$t][count($ary[$t])-1];<br /> } else $cv=&amp;$ary[$t];<br /> if (isset($r['attributes'])) {foreach ($r['attributes'] as $k=&gt;$v) $cv['_a'][$k]=$v;}<br /> $cv['_c']=array();<br /> $cv['_c']['_p']=&amp;$ary;<br /> $ary=&amp;$cv['_c'];<br /><br /> } elseif ($r['type']=='complete') {<br /> if (isset($ary[$t])) { // same as open<br /> if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array());<br /> $cv=&amp;$ary[$t][count($ary[$t])-1];<br /> } else $cv=&amp;$ary[$t];<br /> if (isset($r['attributes'])) {foreach ($r['attributes'] as $k=&gt;$v) $cv['_a'][$k]=$v;}<br /> $cv['_v']=(isset($r['value']) ? $r['value'] : '');<br /><br /> } elseif ($r['type']=='close') {<br /> $ary=&amp;$ary['_p'];<br /> }<br /> } <br /> <br /> _del_p($mnary);<br /> return $mnary;<br />}<br /><br />// _Internal: Remove recursion in result array<br />function _del_p(&amp;$ary) {<br /> foreach ($ary as $k=&gt;$v) {<br /> if ($k==='_p') unset($ary[$k]);<br /> elseif (is_array($ary[$k])) _del_p($ary[$k]);<br /> }<br />}<br /><br />// Array to XML<br />function ary2xml($cary, $d=0, $forcetag='') {<br /> $res=array();<br /> foreach ($cary as $tag=&gt;$r) {<br /> if (isset($r[0])) {<br /> $res[]=ary2xml($r, $d, $tag);<br /> } else {<br /> if ($forcetag) $tag=$forcetag;<br /> $sp=str_repeat(&quot;\t&quot;, $d);<br /> $res[]=&quot;$sp&lt;$tag&quot;;<br /> if (isset($r['_a'])) {foreach ($r['_a'] as $at=&gt;$av) $res[]=&quot; $at=\&quot;$av\&quot;&quot;;}<br /> $res[]=&quot;&gt;&quot;.((isset($r['_c'])) ? &quot;\n&quot; : '');<br /> if (isset($r['_c'])) $res[]=ary2xml($r['_c'], $d+1);<br /> elseif (isset($r['_v'])) $res[]=$r['_v'];<br /> $res[]=(isset($r['_c']) ? $sp : '').&quot;&lt;/$tag&gt;\n&quot;;<br /> }<br /> <br /> }<br /> return implode('', $res);<br />}<br /><br />// Insert element into array<br />function ins2ary(&amp;$ary, $element, $pos) {<br /> $ar1=array_slice($ary, 0, $pos); $ar1[]=$element;<br /> $ary=array_merge($ar1, array_slice($ary, $pos));<br />}<br /></pre><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/312300893061964297-5926240885823859531?l=mysrc.blogspot.com'/></div>leoSrhttp://www.blogger.com/profile/09731107791619864392noreply@blogger.com44