Home > Software engineering >  How to split XML child into multiple children based on a delimiter using PHP
How to split XML child into multiple children based on a delimiter using PHP

Time:02-16

I have an XML child which contains a bunch of URLs which are delimited by '|'

ie.

<ImageURL>https://example.com/example.jpg|https://example.com/example2.jpg</ImageURL>

I'm trying to write a PHP function which will take each URL and split it into it's own child element. So it should look like below:

<ImageURL>
  <Image1>https://example.com/example.jpg</Image1>
  <Image2>https://example.com/example2.jpg</Image2>
  ..etc
</ImageURL>

The data is passed into the function as $value which is the contents of that ImageURL. Not entirely sure where to start though. Any assistance would be appreciated!

function split_images($value){
  ...
}

CodePudding user response:

It's a bit messy but it works.

<?php
    $xml = '<ImageURL>https://example.com/example.jpg|https://example.com/example2.jpg</ImageURL>';
    //the first `explode` removes the parent tags <ImageURL> so that we can get the child url inside as an array
    $childUrl = explode('|',explode('>',$xml)[1]);
    //I used `var_dump` to make sure that I only get child image `src`
    var_dump($childUrl); //output = array(2) ['https://example.com/example.jpg','https://example.com/example2.jpg']
    //then we created a concatinated parent tag `<imageURL>`
    $imgUrl = '<ImageURL>';
    foreach($childUrl as $key => $value){
        //while using a foreach loop to assign the child url as '<Image></Image>' src
        $imgUrl .= '<Image' . ($key   1) . '> ' . $value . '</Image' . ($key   1) . '>';
    }
    $imgUrl .= '<ImageURL>';
    //I used `var_dump` again to test if I get the results you are looking for...
    var_dump($imgUrl); //output = string(132) "<ImageURL><Image1>https://example.com/example.jpg</Image1><Image2> https://example.com/example2.jpg</ImageURL</Image2><ImageURL>"

As you can see, the results are in a string format, you can convert then into json or anything you are looking for.

Check it out here PHP Sandbox

If it is not what you are looking for, hopefully it will give you some general idea.

CodePudding user response:

Not that difficult with DOM:

$xml = <<<'XML'
<ImageURL>https://example.com/example.jpg|https://example.com/example2.jpg</ImageURL>
XML;

$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);

// iterate the ImgeURL elements
foreach ($xpath->evaluate('//ImageURL') as $imageURL) {
    // read the content and separate the URLs
    $urls = explode('|', $imageURL->textContent);
    // remove the content from the node
    $imageURL->textContent = '';
    // iterate the urls
    foreach ($urls as $index => $url) {
        // add a new Image node and set the url as content
        $imageURL
            ->appendChild($document->createElement('Image'))
            ->textContent = $url;
    }
}

$document->formatOutput = TRUE;
echo $document->saveXML();

Numbered (and dynamic) XML are an anti pattern. You should not do this, however you could add the $index to the element name.

  • Related