无法将表单的值写入文件
Can't write values of a form to file
我有一个表格。像这样:
代码:
<form action="rewrite.php" method="get">
<?php
foreach ($tomb2[1] as $key => $metaname){
$talalat = $tomb[1][$key];
echo '<p>' . "$metaname\n" . '</p>' . '<br>' . '<input type="text" name="metavalue[]" value="' . "$talalat\n" . '">' . '<br>';
}
?>
<input type="submit" name="Generálás" value="insert" onclick="insert()" />
</form>
需要将表单的值写入 xml 文件:
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="ALALALAL">4
</property>
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="3" name="MACABSZ">4
</property>
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="4" name="3">4
</property
><property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="5" name="4">4</property></Properties>
我使用以下 php 代码进行替换:
<?php
$ertekek = $_GET["metavalue"];
foreach ($ertekek as $alma ){
$rewrite= file_get_contents('docs/sablonTeszt20150805/docProps/custom.xml');
$rewrite = preg_replace('_<vt:lpwstr>(.*?)</vt:lpwstr>_', "$alma\n" , $rewrite);
file_put_contents('docs/sablonTeszt20150805/docProps/custom.xml', $rewrite );
}
?>
代码有效,但只有表单的最后一个值写入 xml 文件。我需要以下 xml:
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="ALALALAL">1
</property>
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="3" name="MACABSZ">2
</property>
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="4" name="3">3
</property
><property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="5" name="4">4</property></Properties>
我做错了什么?
preg_replace
函数默认替换所有正则表达式的匹配。因此在第一次迭代之后,您的 XML
中不再有 <vt:lpwstr>
标签,因此任何后续迭代都不会有任何匹配项,并且不会再更改 XML
。
基本修复是使用 preg_replace
的可选 limit
参数,并表明您只需要一个替换:
$rewrite = preg_replace('_<vt:lpwstr>(.*?)</vt:lpwstr>_',
"$alma\n", $rewrite, 1);
现在您可以进行一些小的改进:
首先,每次迭代都要读写文件,很浪费磁盘I/O。只做一次,在循环之外。
其次,您需要删除替换值前后保留的任何白色-space。
把所有这些放在一起,你会得到这个:
$ertekek = $_GET["metavalue"];
$rewrite = file_get_contents('docs/sablonTeszt20150805/docProps/custom.xml');
foreach ($ertekek as $alma) {
$rewrite = preg_replace('_\s*<vt:lpwstr>(.*?)</vt:lpwstr>\s*_',
"$alma", $rewrite, 1);
}
file_put_contents('docs/sablonTeszt20150805/docProps/custom.xml', $rewrite);
仍然存在一个潜在的弱点,因为此代码依赖于 metavalues
迭代的特定顺序。我不确定在执行的所有步骤中都能保证这一点。
最后一点,正则表达式不是操作 XML
文件的理想方式。 DOMDocument class in php 以可控的方式提供您所需的一切。
我有一个表格。像这样:
代码:
<form action="rewrite.php" method="get">
<?php
foreach ($tomb2[1] as $key => $metaname){
$talalat = $tomb[1][$key];
echo '<p>' . "$metaname\n" . '</p>' . '<br>' . '<input type="text" name="metavalue[]" value="' . "$talalat\n" . '">' . '<br>';
}
?>
<input type="submit" name="Generálás" value="insert" onclick="insert()" />
</form>
需要将表单的值写入 xml 文件:
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="ALALALAL">4
</property>
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="3" name="MACABSZ">4
</property>
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="4" name="3">4
</property
><property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="5" name="4">4</property></Properties>
我使用以下 php 代码进行替换:
<?php
$ertekek = $_GET["metavalue"];
foreach ($ertekek as $alma ){
$rewrite= file_get_contents('docs/sablonTeszt20150805/docProps/custom.xml');
$rewrite = preg_replace('_<vt:lpwstr>(.*?)</vt:lpwstr>_', "$alma\n" , $rewrite);
file_put_contents('docs/sablonTeszt20150805/docProps/custom.xml', $rewrite );
}
?>
代码有效,但只有表单的最后一个值写入 xml 文件。我需要以下 xml:
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="ALALALAL">1
</property>
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="3" name="MACABSZ">2
</property>
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="4" name="3">3
</property
><property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="5" name="4">4</property></Properties>
我做错了什么?
preg_replace
函数默认替换所有正则表达式的匹配。因此在第一次迭代之后,您的 XML
中不再有 <vt:lpwstr>
标签,因此任何后续迭代都不会有任何匹配项,并且不会再更改 XML
。
基本修复是使用 preg_replace
的可选 limit
参数,并表明您只需要一个替换:
$rewrite = preg_replace('_<vt:lpwstr>(.*?)</vt:lpwstr>_',
"$alma\n", $rewrite, 1);
现在您可以进行一些小的改进:
首先,每次迭代都要读写文件,很浪费磁盘I/O。只做一次,在循环之外。
其次,您需要删除替换值前后保留的任何白色-space。
把所有这些放在一起,你会得到这个:
$ertekek = $_GET["metavalue"];
$rewrite = file_get_contents('docs/sablonTeszt20150805/docProps/custom.xml');
foreach ($ertekek as $alma) {
$rewrite = preg_replace('_\s*<vt:lpwstr>(.*?)</vt:lpwstr>\s*_',
"$alma", $rewrite, 1);
}
file_put_contents('docs/sablonTeszt20150805/docProps/custom.xml', $rewrite);
仍然存在一个潜在的弱点,因为此代码依赖于 metavalues
迭代的特定顺序。我不确定在执行的所有步骤中都能保证这一点。
最后一点,正则表达式不是操作 XML
文件的理想方式。 DOMDocument class in php 以可控的方式提供您所需的一切。