I have a form. Something like this:
Code:
<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>
The values of the form are needed to be written to an xml file:
<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>
To the replace I use the following php code:
<?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 );
}
?>
The code is working, however only the last value of the form is written to the xml file. I need the following 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>
What am I doing wrong?
The preg_replace
function by default replaces all matches of the regular expression. Therefore after the first iteration, there are no more <vt:lpwstr>
tags in your XML
, and so any following iteration will not have any matches, and will not change the XML
any more.
The basic fix is using the optional limit
argument of preg_replace
, and indicate you only want one replacement:
$rewrite = preg_replace('_<vt:lpwstr>(.*?)</vt:lpwstr>_',
"$alma\n", $rewrite, 1);
Now there are several small improvements you could make:
First, it is a waste of disk I/O to read and write the file in each iteration. Just do it once, outside the loop.
Secondly, you'll want to remove any white-space that would remain before and after the replaced value.
Putting that all together, you would get this:
$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);
There is a potential weakness still, in that this code relies on a certain order of the metavalues
iterated. I am not sure this is guaranteed across all steps performed.
On a final note, regular expressions are not the ideal way to manipulate XML
files. The DOMDocument class in php provides all you need to do that in a controlled way.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.