0

There is a problem with preg_replace while parsing an XML file.

<?xml version="1.0" encoding="UTF-8"?>
    <Products>
        <Product>
            <!--- .. -->
            <Properties>
            <!--- .. -->
                <Property>
                    <Id>bdfe66a6-707f-11e4-bac6-50b7c36a6683</Id>
                    <Title>m3</Title>
                    <Value>2,25</Value>
                </Property>
            </Properties>
           <!---..-->
           <Packages>
                <Package>
                    <Id>bdfe66a6-707f-11e4-bac6-50b7c36a6683</Id>
                    <Title>m3</Title>
                    <Value>2,25</Value>
                </Package>
            </Packages>
        </Product>
    </Products>

Here is PHP script, which opens a file, changes special characters, searches the file for matches and then replaces them with specified strings:

<?php

//Replacing symbols < > with &lt; and &gt;
$xml_origin = str_replace("<","&lt;",str_replace(">","&gt;",file_get_contents("import.xml"))); 

//Searching for all matches
preg_match_all("/((\&lt;)Packages>\s*
 (\&lt;)Id>\s*.*?(\&lt;)\/Id>\s*
 (\&lt;)Title>\s*.*?(\&lt;)\/Title>\s*
 (\&lt;)Value>.*?(\&lt;)\/Value>\s*)/",
$xml_origin, $matches, PREG_PATTERN_ORDER);

//Making an array: key - ID, value - Title
foreach ($matches[0] as $key => $val) {
    preg_match("/(\&lt;)Id(\&gt;)\s*.*?(\&lt;)\/Id(\&gt;)\s*/",
        $val, $ids);
    $proc_ids[] = $ids[0];
    preg_match("/(\&lt;)Title(\&gt;)\s*.*?(\&lt;)\/Title(\&gt;)/",
        $val, $titles);
    $proc_titles[] = $titles[0];
}
$data = array_combine($proc_ids, $proc_titles);

//Making an array with replacing strings
foreach ($data as $id => $title) {
    $search_line[] = "/((\&lt;)Property(\&gt;)\s*".trim($id)."\s*".trim($title).")/";
    if ($title == "&lt;Title&gt;m3&lt;/Title&gt;"){
         $match_line[] = "&lt;Property&gt; &lt;Id&gt;some_id1&lt;/Id&gt; ".trim($title);
    }
    elseif ($title == "&lt;Title&gt;m2&lt;/Title&gt;") {
         $match_line[] = "&lt;Property&gt; &lt;Id&gt;some_id2&lt;/Id&gt; ".trim($title);
    }
    elseif ($title == "&lt;Title&gt;un&lt;/Title&gt;") {
         $match_line[] = "&lt;Property&gt; &lt;Id&gt;some_id3&lt;/Id&gt; ".trim($title);
    }
}

//Replacing strings
$xml_processed = preg_replace($search_line,$match_line, $xml_origin);
print_r($xml_processed);
?>

The main problem is that it returns an empty page. Apparently preg_replace returns an error, so it's output is empty.

I know that I should use XML parsers like SimpleXML for this purpose, but I didn't get it enough to write something like that.

I will be grateful for any help you can provide.

Syrcon
  • 1
  • 1
  • 3
    http://stackoverflow.com/a/1732454/2265374 Using an XML Parser and Xpath is a lot easier. – ThW Nov 23 '15 at 13:45
  • I second what @ThW said. Even though I'm a huge fan of regular expressions and write them whenever I can, I'd say that trying to reinvent a pre-existing parser would be more difficult than taking the time to actually understand said technologies you attempted, but didn't succeed. If you want a challenge, I'd say try tackling this one on your own (if you're new to regex). Otherwise, I'd take the shortcut. – Erutan409 Nov 23 '15 at 14:17
  • 1
    @ThW Oh, okay. Thanks a lot. Well, I really should give a SimpleXML parser second chance. –  Syrcon Nov 23 '15 at 14:29
  • 1
    @Erutan409 thank you too –  Syrcon Nov 23 '15 at 14:29

0 Answers0