我正在编写一些读取XML模板的PHP代码,并用PHP中的数组中的数据替换模板中的某些值。目前,当执行代码时,它将为循环的每次迭代输出一个文件,每个迭代都替换了正确的值。
我面临的问题是尝试将每个输出都放到同一个XML文档上,而不是每个输出一个。以下是目前的代码;
<?
$filename = 'MYDRIVE\XML_TEMPLATE2.xml';
$contents = file_get_contents($filename);
$emaillist = array(array('fname' => 'Brad',
'lname' => 'BoBo',
'recipient' => 'bbobo@gmail.com'),
array('fname' => 'Josh',
'lname' => 'Jojo',
'recipient' => 'jjojo@gmail.com'),
array('fname' => 'Sam',
'lname' => 'Soso',
'recipient' => 'ssoso@gmail.com'),
array('fname' => 'Dave',
'lname' => 'Dojo',
'recipient' => 'ddojo@hotmail.com'));
foreach ($emaillist as &$person)
{
$fname = $person['fname'];
$lname = $person['lname'];
$recipient = $person['recipient'];
$todaysdate = date("F j, Y, g:i a");
$find = array("[[firstname]]",
"[[lastname]]",
"[[recipient]]",
"[[todaysdate]]");
$replace = array($fname,
$lname,
$recipient,
$todaysdate);
$new = str_replace($find,
$replace,
$contents);
// This will open/ create the file if non-existent.
$handle = fopen("MYDRIVE/tempname.xml", "w+");
$filename = 'MYDRIVE/tempname.xml';
$filecontent = $new;
if (is_writable($filename))
{
print "<b>Data added to file:</b>";
print "<br>";
print "<br>";
if (!$handle = fopen($filename, 'a'))
{
print "Cannot open file ($filename)";
exit;
}
// Adds $filecontent to file.
if (fwrite($handle, $filecontent) === FALSE)
{
print "Cannot write to file ($filename)";
exit;
}
print "Success! <br><br> ($filecontent) <br><br> was added to ($filename)";
fclose($handle);
}
else
{
print "<b>Error:</b>";
print "The file $filename is not writable";
exit();
}
// Generate file name for use.
$filenamegen = date("d_h_i_s") . "_" . $person['recipient'];
if (rename ("MYDRIVE/tempname.xml", "MYDRIVE/" . $filenamegen . ".xml"))
{
print "File successfully saved as : $filenamegen";
print "<br><br>";
}
else
{
print "Error renaming file.";
exit();
}
print "</div>";
}
?>
执行上述代码时,会创建4个XML文档,并为每个数据记录正确替换变量。
我面临的问题是尝试将所有数据放入单个XML文件中。如果有人可以就此提出任何建议,我将不胜感激。
一如既往地谢谢,
答案 0 :(得分:1)
有几个人认为我可以做得更好,首先正如Andreas所说,你在每个循环中重新分配var $ new,所以请记住一个concat运算符点:
$new .= str_replace($find,$replace,$contents);
并且在循环结束时,您将在$ new上连接所有循环迭代。
我建议您打开文档:
<?php
而不是:
<?
通过这种方式,php将能够使用XML。
我建议使用特定的库来处理XML而不是你正在做的事情。
更好的想法是构建你的xml并解析/保存使用磁盘工作的内容...将来你会遇到很多麻烦,特殊的字符,字符集......等等。
答案 1 :(得分:0)
试试这个(未经测试):
<?php
$filename = 'MYDRIVE\XML_TEMPLATE2.xml';
$contents = file_get_contents($filename);
$emaillist = array(array('fname' => 'Brad',
'lname' => 'BoBo',
'recipient' => 'bbobo@gmail.com'),
array('fname' => 'Josh',
'lname' => 'Jojo',
'recipient' => 'jjojo@gmail.com'),
array('fname' => 'Sam',
'lname' => 'Soso',
'recipient' => 'ssoso@gmail.com'),
array('fname' => 'Dave',
'lname' => 'Dojo',
'recipient' => 'ddojo@hotmail.com'));
// This will open/ create the file if non-existent.
$handle = fopen("MYDRIVE/tempname.xml", "w+");
$filename = 'MYDRIVE/tempname.xml';
$filecontent = '';
foreach ($emaillist as &$person)
{
$fname = $person['fname'];
$lname = $person['lname'];
$recipient = $person['recipient'];
$todaysdate = date("F j, Y, g:i a");
$find = array("[[firstname]]",
"[[lastname]]",
"[[recipient]]",
"[[todaysdate]]");
$replace = array($fname,
$lname,
$recipient,
$todaysdate);
$new = str_replace($find,
$replace,
$contents);
$filecontent .= $new;
}
if (is_writable($filename))
{
print "<b>Data added to file:</b>";
print "<br>";
print "<br>";
if (!$handle = fopen($filename, 'a'))
{
print "Cannot open file ($filename)";
exit;
}
// Adds $filecontent to file.
if (fwrite($handle, $filecontent) === FALSE)
{
print "Cannot write to file ($filename)";
exit;
}
print "Success! <br><br> ($filecontent) <br><br> was added to ($filename)";
fclose($handle);
}
else
{
print "<b>Error:</b>";
print "The file $filename is not writable";
exit();
}
// Generate file name for use.
$filenamegen = date("d_h_i_s");
if (rename ("MYDRIVE/tempname.xml", "MYDRIVE/" . $filenamegen . ".xml"))
{
print "File successfully saved as : $filenamegen";
print "<br><br>";
}
else
{
print "Error renaming file.";
exit();
}
print "</div>";
如果您的XML_TEMPLATE2.xml
中有XML标题,则应将其删除,然后将其添加到$filecontent
foreach
- 循环之前。对于根节点也是如此 - 在foreach
之前将其打开并在foreach
之后关闭它。