I have a xml file with attributes
XML Document
<SET name="John" lastname="Steve" city="New York" Country=""/>
This is converted to CSV file with 4 colums.
CSV File
Name,LastName,City,Country
John,Steve,New York,
I want to get the Empty content from Country and convert to a default text like USA
I will use str_replace to do this replacement.
The problem is that i don’t know how to get the Empty attribute, like if is empy var_dump.
Thanks
Conversion file.
$filexml = 'file.xml';
if (file_exists($filexml)){
$xml = simplexml_load_file($filexml);
$file = fopen('clients.csv', 'w');
$header = array('Name', 'LastName', 'City', 'Country');
fputcsv($file, $header, ',', '"');
foreach ($xml->SET as $set){
$item = array();
$value1 = $set->attributes()->name;
$value2 = $set->attributes()->lastname;
$value3 = $set->attributes()->city;
$value4 = $set->attributes()->Country;
$search_country= array('','10','12')
$replace_country = array('USA','Argentina','Canada')
$item[] = $value1;
$item[] = $value2;
$item[] = $value3;
$item[] = str_replace($search_country, $replace_country, $value4);
fputcsv($file, $item, ',', '"');
}
fclose($file);
}
Use
emptyto check if the values are empty