I have this csv2xml.php file

<?php


error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", true);

ini_set('memory_limit', '24M');



function csv2xml($file, $container = 'data')
{
$r = "<{$container}>\n";
$row = 0;
$cols = 0;
$titles = array();

$handle = @fopen($file, 'r');
if (!$handle) return $handle;

while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
{

if (!$cols) $cols = count($data);
for ($i = 0; $i < $cols; $i++)
{
if ($row == 0)
{
$titles[$i] = $data[$i];
continue;
}

$r .= "\t\t<{$titles[$i]}>";
$r .= $data[$i];
$r .= "</{$titles[$i]}>\n";
}

$row++;
}
fclose($handle);
$r .= "</{$container}>";

return $r;
}








$xml = '<?xml version="1.0" encoding="ISO-8859-1" ?> ';
$xml .= csv2xml('dtifeed.csv', 'petad');



$xmlfile = @fopen('dtifeed.xml', 'wb') or die('Could not open XML file for writing');

fwrite($xmlfile, $xml) or die('Could not write string to XML file');

fclose($xmlfile);

echo "Successfully wrote the XML file";

?>

And I get this results

<?xml version="1.0" encoding="ISO-8859-1" ?> <petad>
		<ID>456493</ID>
		<Category>P105</Category>
		<></>
		<></>
		<Description>DOGS FOR SALE - They are the best dogs ever. This is a test ad, I would never sell my dogs. Test ad. Please call 928-555-1212.
</Description>
		<ID>456501</ID>
		<Category>P105</Category>
		<></>
		<></>
		<Description>FIDO MUST GO - But not because we don't love him. Because this is a test ad. The best test ad you've ever read. $1,000. 928-555-1212
</Description>
		<ID>456503</ID>
		<Category>P110</Category>
		<></>
		<></>
		<Description>KITTENS FREE TO A GOOD HOME - All must go together! There are 101 of them. 928-555-6868.456504</Description>
		<ID>456514</ID>
		<Category>P125</Category>
		<></>
		<></>
		<Description>A HORSE IS A HORSE unless it's a test ad. Horses for sale, $8,000.  928-111-2323.456516</Description>
</petad>

How can I get rid of the empty tags?

Thank you

this should do it.

<?php


error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", true);

ini_set('memory_limit', '24M');



function csv2xml($file, $container = 'data')
{
$r = "<{$container}>\n";
$row = 0;
$cols = 0;
$titles = array();

$handle = @fopen($file, 'r');
if (!$handle) return $handle;

while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
{

if (!$cols) $cols = count($data);
for ($i = 0; $i < $cols; $i++)
{
if ($row == 0)
{
$titles[$i] = $data[$i];
continue;
}

$r .= trim($data[$i]) != ""?"\t\t<{$titles[$i]}>$data[$i]</{$titles[$i]}>\n":"";
}

$row++;
}
fclose($handle);
$r .= "</{$container}>";

return $r;
}








$xml = '<?xml version="1.0" encoding="ISO-8859-1" ?> ';
$xml .= csv2xml('dtifeed.csv', 'petad');



$xmlfile = @fopen('dtifeed.xml', 'wb') or die('Could not open XML file for writing');

fwrite($xmlfile, $xml) or die('Could not write string to XML file');

fclose($xmlfile);

echo "Successfully wrote the XML file";

?>

This worked

I also added some code to create a ne file every day and write the xml content there

thank you

Hello, lets say that I write an email address in the description field, is there any way to get that email and place it in the xml as a <email></email>

But if I dont have an email in the description just avoid this and continue?

is this possible ?

You can explode the description field with explode(" ", $description) and then loop through the array with a regular expression(just like you would with an email text field validation). In the case that you do get one or more that comes up true, add them to an emails array and after you are done with the description you can then loop through the emails array echoing them with <email> and </email> surrounding email email address.

make sense?

You can explode the description field with explode(" ", $description) and then loop through the array with a regular expression(just like you would with an email text field validation). In the case that you do get one or more that comes up true, add them to an emails array and after you are done with the description you can then loop through the emails array echoing them with <email> and </email> surrounding email email address.

make sense?

Yes do you have a sample

Yes do you have a sample

I also made some adjustments to help keep it xml compliant

<?php


error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", true);

ini_set('memory_limit', '24M');



function csv2xml($file, $container = 'data')
{
	$dataarray = array();
	$r = "<{$container}>\n";
	
	$row = 0;
	$cols = 0;
	$titles = array();
	
	$handle = @fopen($file, 'r');
	if (!$handle) return $handle;
	
	while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
	{
	
		if (!$cols) $cols = count($data);
		for ($i = 0; $i < $cols; $i++)
		{
			if ($row == 0)
			{
				$titles[$i] = $data[$i];
				continue;
			}
			
			$emailsarray = array();
			if(strtolower($titles[$i]) == "description")
			{
				foreach(explode(" ", $data[$i]) as $value)
				{
					if(eregi("^[a-zA-Z0-9/\._\-]+@[a-zA-Z0-9_\-]+\.[a-zA-Z0-9\._\-]+$", $value))
					{
						$emailsarray[] = $value;
					}
				}
			}
			
			$dataarray[$titles[$i]][] = $data[$i];
			
			foreach($emailsarray as $value)
			{
				$dataarray["email"][] = $value;
			}
		}
		
		$row++;
	}
	fclose($handle);
	
	$containers = array("email");
	
	foreach($dataarray as $key=>$value)
	{
		if(in_array(strtolower($key), $containers))
		{
			$r .= "\t\t<" . $key . "-container>\n";
		}
		
		foreach($value as $value2)
		{
			$tabchars = "\t\t";
			if(in_array(strtolower($key), $containers))
			{
				$tabchars .= "\t\t";
			}
			$r .= $tabchars . "<$key>$value2</$key>\n";
		}
		
		if(in_array(strtolower($key), $containers))
		{
			$r .= "\t\t</" . $key . "-container>\n";
		}
	}
	
	$r .= "</{$container}>\n";
	return $r;
}








$xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
$xml .= csv2xml('dtifeed.csv', 'petad');
echo "<xmp>" . $xml . "</xmp>";


$xmlfile = @fopen('dtifeed.xml', 'wb') or die('Could not open XML file for writing');

fwrite($xmlfile, $xml) or die('Could not write string to XML file');

fclose($xmlfile);

echo "Successfully wrote the XML file";

?>

Hello I added my code to your code so I can be able to write to the new xml file and also the trim function and is not working it gives me a blank page

<?php


error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", true);

ini_set('memory_limit', '24M');

$file = "pets_feed_" . date("Ymd") . ".xml";// - for yyyymmdd
  
if (!file_exists($file)) touch($file);

$fh = fopen($file, "r");

function csv2xml($file, $container = 'data')
{
	$dataarray = array();
	$r = "<{$container}>\n";
	
	$row = 0;
	$cols = 0;
	$titles = array();
	
	$handle = @fopen($file, 'r');
	if (!$handle) return $handle;
	
	while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
	{
	
		if (!$cols) $cols = count($data);
		for ($i = 0; $i < $cols; $i++)
		{
			if ($row == 0)
			{
				$titles[$i] = $data[$i];
				continue;
			}
$r .= trim($data[$i]) != ""?"\t\t<{$titles[$i]}>$data[$i]</{$titles[$i]}>\n":"";
}
			
			$emailsarray = array();
			if(strtolower($titles[$i]) == "description")
			{
				foreach(explode(" ", $data[$i]) as $value)
				{
					if(eregi("^[a-zA-Z0-9/\._\-]+@[a-zA-Z0-9_\-]+\.[a-zA-Z0-9\._\-]+$", $value))
					{
						$emailsarray[] = $value;
					}
				}
			}
			
			$dataarray[$titles[$i]][] = $data[$i];
			
			foreach($emailsarray as $value)
			{
				$dataarray["email"][] = $value;
			}
		}
		
		$row++;
	}
	fclose($handle);
	
	$containers = array("email");
	
	foreach($dataarray as $key=>$value)
	{
		if(in_array(strtolower($key), $containers))
		{
			$r .= "\t\t<" . $key . "-container>\n";
		}
		
		foreach($value as $value2)
		{
			$tabchars = "\t\t";
			if(in_array(strtolower($key), $containers))
			{
				$tabchars .= "\t\t";
			}
			$r .= $tabchars . "<$key>$value2</$key>\n";
		}
		
		if(in_array(strtolower($key), $containers))
		{
			$r .= "\t\t</" . $key . "-container>\n";
		}
	}
	
	$r .= "</{$container}>\n";
	return $r;
}








$xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
$xml .= csv2xml('petsfeed.csv', 'petad');
echo "<xmp>" . $xml . "</xmp>";


$xmlfile = @fopen('dtifeed.xml', 'wb') or die('Could not open XML file for writing');

fwrite($xmlfile, $xml) or die('Could not write string to XML file');

fclose($xmlfile);

echo "Successfully wrote the XML file";

?>

I discover the problem this is

$r .= trim($data[$i]) != ""?"\t\t<{$titles[$i]}>$data[$i]</{$titles[$i]}>\n":"";

DO I need to name r different???

I discover the problem this is

$r .= trim($data[$i]) != ""?"\t\t<{$titles[$i]}>$data[$i]</{$titles[$i]}>\n":"";

DO I need to name r different???

Oops, sorry, forgot about that line, this should do it.

<?php


error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", true);

ini_set('memory_limit', '24M');

$file = "pets_feed_" . date("Ymd") . ".xml";// - for yyyymmdd
  
if (!file_exists($file)) touch($file);

$fh = fopen($file, "r");

function csv2xml($file, $container = 'data')
{
	$dataarray = array();
	$r = "<{$container}>\n";
	
	$row = 0;
	$cols = 0;
	$titles = array();
	
	$handle = @fopen($file, 'r');
	if (!$handle) return $handle;
	
	while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
	{
	
		if (!$cols) $cols = count($data);
		for ($i = 0; $i < $cols; $i++)
		{
			if ($row == 0)
			{
				$titles[$i] = $data[$i];
				continue;
			}
			
			$emailsarray = array();
			if(strtolower($titles[$i]) == "description")
			{
				foreach(explode(" ", $data[$i]) as $value)
				{
					if(eregi("^[a-zA-Z0-9/\._\-]+@[a-zA-Z0-9_\-]+\.[a-zA-Z0-9\._\-]+$", $value))
					{
						$emailsarray[] = $value;
					}
				}
			}
			
			$dataarray[$titles[$i]][] = trim($data[$i]) != ""?$data[$i]:"";
			
			foreach($emailsarray as $value)
			{
				$dataarray["email"][] = $value;
			}
		}
		
		$row++;
	}
	fclose($handle);
	
	$containers = array("email");
	
	foreach($dataarray as $key=>$value)
	{
		if(in_array(strtolower($key), $containers))
		{
			$r .= "\t\t<" . $key . "-container>\n";
		}
		
		foreach($value as $value2)
		{
			$tabchars = "\t\t";
			if(in_array(strtolower($key), $containers))
			{
				$tabchars .= "\t\t";
			}
			$r .= $tabchars . "<$key>$value2</$key>\n";
		}
		
		if(in_array(strtolower($key), $containers))
		{
			$r .= "\t\t</" . $key . "-container>\n";
		}
	}
	
	$r .= "</{$container}>\n";
	return $r;
}








$xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
$xml .= csv2xml('petsfeed.csv', 'petad');


$xmlfile = @fopen('dtifeed.xml', 'wb') or die('Could not open XML file for writing');

fwrite($xmlfile, $xml) or die('Could not write string to XML file');

fclose($xmlfile);

echo "Successfully wrote the XML file";

?>

it has been changed to this line
$dataarray[$titles[$i]][] = trim($data[$i]) != ""?$data[$i]:"";

The reason I added the data to $dataarray is because it is possible now to have multiple email addresses and it is (I believe) xml compliant that when you have multiple of anything that they should go into a container. Or maybe it is just good practice.

This line here defines your containers:
$containers = array("email");

Another thing that this does is if you have an email field and you are also pulling email addresses out of the description, you want them to be grouped together, the $dataarray allows you to do that by adding all of the email address to $dataarray key and then looping through after all the data is looped through.

You know how I am writting this file to my server every day

#
$file = "pets_feed_" . date("Ymd") . ".xml";// - for yyyymmdd

is there any way to delete an old file when the new one is uploaded every day

???

Thank you

Hold on, something still isn't right. I am still working on this.

OK that's better, had to add another dimension to the array to loop through the rows, you probably noticed that. Try this.

<?php


error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", true);

ini_set('memory_limit', '24M');



function csv2xml($file, $container = 'data')
{
	$dataarray = array();
	$r = "<{$container}>\n";
	
	$row = 0;
	$cols = 0;
	$titles = array();
	
	$handle = @fopen($file, 'r');
	if (!$handle) return $handle;
	
	while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
	{
	
		if (!$cols) $cols = count($data);
		for ($i = 0; $i < $cols; $i++)
		{
			if ($row == 0)
			{
				$titles[$i] = $data[$i];
				continue;
			}
			
			$emailsarray = array();
			if(strtolower($titles[$i]) == "description")
			{
				foreach(explode(" ", $data[$i]) as $value)
				{
					if(eregi("^[a-zA-Z0-9/\._\-]+@[a-zA-Z0-9_\-]+\.[a-zA-Z0-9\._\-]+$", $value))
					{
						$emailsarray[] = $value;
					}
				}
			}
			
			$dataarray[$row][$titles[$i]][] = trim($data[$i]) != ""?$data[$i]:"";
			
			foreach($emailsarray as $value)
			{
				$dataarray[$row]["email"][] = $value;
			}
		}
		
		$row++;
	}
	fclose($handle);
	
	$containers = array("email");
	
	for($i = 1; $i <= count($dataarray); $i++)
	{
		foreach($dataarray[$i] as $key=>$value)
		{
			if(in_array(strtolower($key), $containers))
			{
				$r .= "\t\t<" . $key . "-container>\n";
			}
			
			foreach($value as $value2)
			{
				$tabchars = "\t\t";
				if(in_array(strtolower($key), $containers))
				{
					$tabchars .= "\t\t";
				}
				$r .= $tabchars . "<$key>$value2</$key>\n";
			}
			
			if(in_array(strtolower($key), $containers))
			{
				$r .= "\t\t</" . $key . "-container>\n";
			}
		}
	}
	
	$r .= "</{$container}>\n";
	return $r;
}








$xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
$xml .= csv2xml('dtifeed.csv', 'petad');


$xmlfile = @fopen('dtifeed.xml', 'wb') or die('Could not open XML file for writing');

fwrite($xmlfile, $xml) or die('Could not write string to XML file');

fclose($xmlfile);

echo "Successfully wrote the XML file";

?>

You know how I am writting this file to my server every day

#
$file = "pets_feed_" . date("Ymd") . ".xml";// - for yyyymmdd

is there any way to delete an old file when the new one is uploaded every day

???

Thank you

You would want to use the unlink() function to delete a file.

Hey Rob at the end I did not need the email thing this is the final copy of code

<?php


error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", true);

ini_set('memory_limit', '24M');


$file = "pets_feed_" . date("Ymd") . ".xml";// - for yyyymmdd
  
if (!file_exists($file)) touch($file);

$fh = fopen($file, "r");

function csv2xml($file, $container = 'data')
{
$r = "<{$container}>\n";
$row = 0;
$cols = 0;
$titles = array();

$handle = @fopen($file, 'r');
if (!$handle) return $handle;

while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
{

if (!$cols) $cols = count($data);
for ($i = 0; $i < $cols; $i++)
{
if ($row == 0)
{
$titles[$i] = $data[$i];
continue;
}

$r .= trim($data[$i]) != ""?"\t\t<{$titles[$i]}>$data[$i]</{$titles[$i]}>\n":"";
}

$row++;
}
fclose($handle);
$r .= "</{$container}>";

return $r;
}








$xml = '<?xml version="1.0" encoding="ISO-8859-1" ?> ';
$xml .= csv2xml('petsfeed.csv', 'petad');



$xmlfile = @fopen($file, "wb") or die('Could not open XML file for writing');

fwrite($xmlfile, $xml) or die('Could not write string to XML file');

fclose($xmlfile);

echo "Successfully wrote the XML file";

?>

The only problem is that I need the <petad> to open and close every ad instead of opening at the beginning of the file and closing at the end 

any ideas?
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.