Reputation: 4491
I currently have a Snom XML file (stored as filename.htm) which I need to use as a template file to produce specific individual files.
I have attempted to load the .htm using get_file_contents and DOMDocument however I either lose the tags in the document or the formatting.
Is there a way to load contents of a file, replace template placeholders then save the new file preserving the format and original spacing?
Template File
<?xml version="1.0" encoding="utf-8" ?>
<settings>
<phone-settings e="2">
<user_active idx="1" perm="R">on</user_active>
<user_realname idx="1" perm="R">{Prefix}{Suffix}</user_realname>
<user_pname idx="1" perm="R">{Prefix}{Suffix}</user_pname>
<user_name idx="1" perm="R">{Prefix}{Suffix}</user_name>
<phone_name idx="1" perm="R">{Prefix}{Suffix}</phone_name>
<user_idle_text idx="1" perm="R">{Prefix}{Suffix}</user_idle_text>
<idle_offhook idx="1" perm="R">on</idle_offhook>
<offhook_dial_prompt idx="1" perm="R">off</offhook_dial_prompt>
</phone-settings>
</settings>
Required End Result
<?xml version="1.0" encoding="utf-8" ?>
<settings>
<phone-settings e="2">
<user_active idx="1" perm="R">on</user_active>
<user_realname idx="1" perm="R">Phone 001</user_realname>
<user_pname idx="1" perm="R">Phone 001</user_pname>
<user_name idx="1" perm="R">Phone 001</user_name>
<phone_name idx="1" perm="R">Phone 001</phone_name>
<user_idle_text idx="1" perm="R">Phone 001</user_idle_text>
<idle_offhook idx="1" perm="R">on</idle_offhook>
<offhook_dial_prompt idx="1" perm="R">off</offhook_dial_prompt>
</phone-settings>
</settings>
Upvotes: 0
Views: 73
Reputation: 360742
Assming the placeholders are unique and not used elsewhere in "non-placeholder" contexts, then a simple string operation would do the trick:
$xml = file_get_contents('template.xml');
$xml = str_replace('{Prefix}', 'Phone', $xml);
$xml = str_replace('{Suffix}', '001', $xml);
etc...
file_put_contents('parsed.xml, $xml);
Upvotes: 1