Skip to content

Instantly share code, notes, and snippets.

@paulofreitas
Created April 6, 2017 20:35
Show Gist options
  • Save paulofreitas/134b2b11bcd0b0e033a99c861aaa577d to your computer and use it in GitHub Desktop.
Save paulofreitas/134b2b11bcd0b0e033a99c861aaa577d to your computer and use it in GitHub Desktop.
<?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