0

XML ファイルと変数があります。特定のノードの値を置き換える関数を書きたいと思います。例:

write_xml("->system_settings->settings->hostname",'Helloooooooo');

function write_xml($tag_address, $value) {

    $xml = simplexml_load_file("test.xml")
                 or die("Error: Cannot create object");
    $xml->system_settings->settings->hostname = $value;

    $xml->asXML("test.xml");
}

この例では、ノード->system_settings->settingsifhellooooooを に置き換える必要がありhostnameます。

私の問題は次のとおりです。上記のコードでは、パスを手動で入力しましたが、機能します。ただし、パスを動的に割り当てると (以下のように)、機能しません。

write_xml("->system_settings->settings->hostname",'Helloooooooo');

function write_xml($tag_address, $value) {

    ...
    $xml->$tag_address = $value; // <---- doesn't work
    $xml->asXML("test.xml");
} 

私は何をすべきか?

4

0 に答える 0