Pravin Pravin - 15 days ago 6
PHP Question

Convert array in a variable and manipulate it to create XML file in php

$bday = $f['date_of_birth']; //it outputs-> 20102016


So I tried using preg_split() or even explode() but was of no help

$str = $bday;
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars[0]);


I want to use $bday as a nodeValue in DOMDocument to create XML file

$doc = new DOMDocument();
$doc->formatOutput = true;

$root = $doc->createElement('StudentDetails');
$root = $doc->appendChild($root);

$ele1 = $doc->createElement('DateOfBirth');
$ele1->nodeValue=$bday;
$root->appendChild($ele1);


I want my XML output to be like

<?xml version="1.0"?>
<StudentDetails>
<DateOfBirth>20/10/2016</DateOfBirth>
</StudentDetails>

Answer

If the date is always DDMMYYYY you could insert slashes with preg_replace.

$bday = preg_replace('/(\d\d)(\d\d)(\d\d\d\d)/', "$1/$2/$3", $bday);