PHP POST as XML

Started on the old forum in May 2012 · view the original in the archive · old links redirect here

No replies · 422 views · started 14 years ago · 1 min read

shushant Developer · 207 posts #1
function print_xml_r($arr,$ wrapper = 'data',$cycle = 1) {
vars $new_line = "\n";
//start building content
if($cycle == 1) {
$output = '<?xml version="1.0"
encoding="UTF-8" ?>'.$new_line; }
$output.= tabify($cycle - 1).'<'.$ wrapper.'>'.$new_line; foreach($arr as $key => $val) { 
if(!is_array($val)) {
$output.= tabify($cycle).'<'.htmlspecialchars($key).'>'.$ val.'</'.htmlspecialchars($key).'>'.$new_line;
}
else
{
$output.= print_xml_r($val,$ key,$cycle + 1).$new_line;
} 
}
$output.= tabify($cycle - 1).'</'.$wrapper.'>';
//return the value
return isset($output)?$output:null;
}
/* tabify */
function tabify($num_tabs) { for($x = 1; $x <= $num_tabs; $x++) {
$return.= "\t";
} 
return $return;
}

Usage

$_POST = array('first_name'=>'Shushant', 'last_name'=>'Kumar', 'url'=>'http://game143.com'', 'languages'=>array ('php','Jquery','Javascript',), 'title'=>'Web Developer', 'favorite_blogs'=>array ('Github'=>'http://github.com','C-Talk'=>'http://coding-talk.com',) ,); 
echo print_xml_r($_POST);

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close