Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicate string from variable in PHP

Tags:

regex

php

I am looking for a way to remove duplicate lines from a variable:

$x = '<IMGURL>one.jpg</IMGURL>';
$x .= '<IMGURL>two.jpg</IMGURL>';
//remove the following line:
$x .= '<IMGURL>one.jpg</IMGURL>';
$x .= '<IMGURL>third.jpg</IMGURL>';

The output should be:

$x = '<IMGURL>one.jpg</IMGURL><IMGURL>two.jpg</IMGURL><IMGURL>third.jpg</IMGURL>';

Maybe some regex does the trick?

Edit:

Some more info

The source XML:

<?xml version=".0" encoding="utf-8"?>
<SHOP>
  <SHOPITEM>
    <name>BLUE product</name>
    <IMGURL>main_picture.jpg</IMGURL>
    <PRODUCT_VARIANT id="2">
      <name>blue L</name>
      <IMGURL>blue.jpg</IMGURL>
    </PRODUCT_VARIANT>
    <PRODUCT_VARIANT id="3">
      <name>BLUE XL</name>
      <IMGURL>blue.jpg</IMGURL>
    </PRODUCT_VARIANT>
    <PRODUCT_VARIANT id="4">
      <name>BLUE XXL</name>
      <IMGURL>blue.jpg</IMGURL>
    </PRODUCT_VARIANT>
  </SHOPITEM>
</SHOP>

From this I need two unique jpg:

  • main_picture.jpg
  • blue.jpg

The interesting part of the module what is processing the source XML:

foreach($xml->SHOPITEM as $product){
if(isset($product->IMGURL)){$xml_content .= '<IMAGE>'.htmlspecialchars($product->IMGURL).'</IMAGE>'."\n";}

foreach($product->variant as $option){
              if(isset($option->IMGURL)){$xml_content .= '<IMAGE>'.htmlspecialchars($option->IMGURL).'</IMAGE>'."\n";}
                      }
}
like image 731
Adrian Avatar asked Mar 17 '26 15:03

Adrian


1 Answers

This sample code reduce your XML to desired result:

$dom = new DOMDocument();
$dom->formatOutput = True;
libxml_use_internal_errors( 1 );
$dom->loadXML( $x, LIBXML_NOBLANKS );

$xpath = new DOMXPath( $dom );

$nodes = $xpath->query( '//SHOP/SHOPITEM/PRODUCT_VARIANT/IMGURL' );
$found = array();

foreach( $nodes as $key => $node )
{
    if( in_array( $node->nodeValue, $found ) )
    { $node->nodeValue = ''; }
    else
    { $found[] = $node->nodeValue; }
}

$result = $dom->saveXML();

3v4l demo

Basically, simply use an array to retrieve unique values and, after retrieving all <IMGURL> nodes through xpath, with a foreach loop check each node: if they exists in array, you set node value to an empty string, otherwise you add current node value to the array.

Above script analyze only <IMGURL> that have <PRODUCT_VARIANT> as parent node; if you want analyze all <IMGURL> nodes, simply change xpath line in:

$nodes = $xpath->query( '*//IMGURL' );
like image 58
fusion3k Avatar answered Mar 20 '26 07:03

fusion3k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!