Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
$xml = <<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<Person>
<Name>{name}</Name>
<Age>{age}</Age>
<Gender>{gender}</Gender>
</Person>
XML;
$data = collect(['name' => 'Paulo', 'age' => 29, 'gender' => 'M']) // $request->all()
->mapWithKeys(function ($value, $key) {
return ["{{$key}}" => $value];
});
var_dump(str_replace($data->keys()->toArray(), $data->values()->toArray(), $xml));
/*
string '<?xml version="1.0" encoding="ISO-8859-1"?>
<Person>
<Name>Paulo</Name>
<Age>29</Age>
<Gender>M</Gender>
</Person>' (length=120)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment