I have a xml, where tags can have one,two or more spaces and periods(.) in elements.
the xml:
$xml='<?xml version="1.0" encoding="UTF-8"?>
<xmldata>
<SalesHeader>
<DocType>Order</DocType>
<No>1002</No>
<SellToCustomerNo>CustNo</SellToCustomerNo>
<SellToCustomerName>Customer Name</SellToCustomerName>
<SellToCustomerName2 />
<SellToEmail>[email protected]</SellToEmail>
<OrderDate>04/03/13</OrderDate>
<ExtDocNo />
<ShipToName>Customer Ship to</ShipToName>
<ShipToCountry />
<TaxLiable>No</TaxLiable>
<TaxAreaCode />
<RequestedDeliveryDate />
<Shipping Agent>UPS</Shipping Agent>
<Shipping Agent Service>Ground New</Shipping Agent Service>
<Tracking Numbers>123123212,1231231321</Tracking Numbers>
<SalesLine>
<ItemNo.>12-34343-23</ItemNo.>
<Description>Item Description</Description>
<Quantity>1</Quantity>
<UnitPrice>79.00</UnitPrice>
</SalesLine>
<SalesLine>
<ItemNo.>12-34343-23</ItemNo.>
<Description>Item Description</Description>
<Quantity>1</Quantity>
<UnitPrice>79.00</UnitPrice>
</SalesLine>
</SalesHeader>
</xmldata>';
my code:
preg_replace(array('/(<\/?)[. ]+(\w*)(\/?>)/','/(<\/?)(\w*)[. ]+(\/?>)/','/(<\/?)(\w*)[. ]+(\w*\/?>)/'),array('$1$2$3','$1$2$3','$1$2$3'),$xml);
I've achieved only delete using preg_match when there is one space or period, but I want is delete periods(.) and replace the spaces with underscore(_) even if there are several periods or/and spaces in tags and any position.
I want to get this:
change:
<ItemNo.>12-34343-23</ItemNo.>
by:
<ItemNo>12-34343-23</ItemNo>
change:
<Shipping Agent>UPS</Shipping Agent>
by
<Shipping_Agent>UPS</Shipping_Agent>
change:
<Shipping Agent Service>Ground New</Shipping Agent Service>
by
<Shipping_Agent_Service>Ground New</Shipping_Agent_Service>
Well, I resolved the problem by my self, this is the code: